新建项目,添加Maven支持。
在pom.xml中添加依赖。
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.7.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.7.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> </dependency> <!-- 如果要使用json序列化 --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>3.0.7.Final</version> </dependency> <!-- 如果要使用xml序列化 --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>3.0.7.Final</version> </dependency> <!-- 如果要使用netty server --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-netty</artifactId> <version>3.0.7.Final</version> </dependency> <!-- 如果要使用Sun HTTP server --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jdk-http</artifactId> <version>3.0.7.Final</version> </dependency> <!-- 如果要使用tomcat server --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.0.11</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-logging-juli</artifactId> <version>8.0.11</version> </dependency>
新建接口
public interface UserService { public ArrayList<String> sayHello(String uid); }
新建接口实现类
@Path("rest") @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) public class UserServiceImpl implements UserService { @GET @Path("/sayHello/{uid}") @Override public ArrayList<String> sayHello(@PathParam("uid")String uid) { ArrayList<String> x= new ArrayList<String>(); x.add("xxxx"); return x; } }
新建dubbox配置文件
<dubbo:application name="platform" owner="programmer" organization="dubbox"/> <dubbo:registry address="zookeeper://localhost:2181" /> <dubbo:protocol name="rest" port="8081" server="tomcat" ></dubbo:protocol> <dubbo:service interface="rest.UserService" ref="userServiceImpl" /> <bean id="userServiceImpl" class="rest.UserServiceImpl"></bean>
新建启动类启动dubbox的rest服务
System.out.println("准备启动"); ClassPathXmlApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext_dubbo.xml"); UserService userSrv=(UserService) ap.getBean("userServiceImpl"); System.out.println("启动DemoServiceProvider完成"); userSrv.sayHello("xushuai"); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); }
验证:启动zookeeper,启动应用。
通过浏览器访问rest接口。