對於和jboss as7的集成不須要作任何工做,jboss默認集成了resteasy,只須要對業務pojo作一些jax-rs的註解標註便可。java
對於非Jboss的Servlet容器
web
Spring和resteasy集成,主要有三種方式,spring
運行於Servlet版本大於等於3.0json
運行於Servlet版本小於3.0api
將resteasy和spring mvc整合在一塊兒服務器
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>jaxrs-api</artifactId> <version>${project.dependency.jboss.resteasy}</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>${project.dependency.jboss.resteasy}</version> </dependency> <!-- Resteasy 集成 Jackson2 工具包 方便JSON 轉換。 --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson2-provider</artifactId> <version>${project.dependency.jboss.resteasy}</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>${project.dependency.jboss.resteasy}</version> </dependency> <!-- Reasteasy 集成Spring g工具包--> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-spring</artifactId> <version>${project.dependency.jboss.resteasy}</version> </dependency>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config.xml</param-value> </context-param>
若是servlet版本大於3.0,那麼須要加入一個servlet的初始化器mvc
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-servlet-initializer</artifactId> <version>${project.dependency.jboss.resteasy}</version> </dependency>
添加2個監聽器,要注意的是,本來Spring默認使用的Context Loader Listener, 就不用使用了,由於第二個監聽器 已經完成了加載Spring的工做了。
app
<!-- Start Resteasy --> <listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <!-- load Spring Context --> <listener> <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class> </listener>
@Path("/restUser") @Named public class RestUserService { @Inject private UserService userServiceImpl; @GET @Path("/list") @Produces(value = {MediaType.APPLICATION_JSON}) public String list() { User user = userServiceImpl.get(4L); return JacksonUtils.get().toJson(user); } }
啓動服務器,訪問 http://ip:port/你的項目路徑/restUser/list。便可獲得相應的json數據啦
ide
當運行的容器Servlet的版本小於3.0 那麼以前的Serlvet3的初始化器是不能正常工做的,須要去掉。工具
要指定前綴不然和spring mvc的url-pattern衝突
<servlet> <servlet-name>resteasy</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rs</param-value> </context-param> </servlet> <servlet-mapping> <servlet-name>resteasy</servlet-name> <url-pattern>/rs/*</url-pattern> </servlet-mapping>
啓動服務器,訪問 http://ip:port/你的項目路徑/restUser/list。便可獲得相應的json數據啦
整合到SpringMVC 是不須要Serlvet 初始化器的
整合到SpringMVC中去,只須要使用SpringMVC的Servlet便可
<servlet> <servlet-name>Spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet;</servlet-class> </servlet> <servlet-mapping> <servlet-name>Spring</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
使用SpringMVC的Controller的類註解,就能夠整合到SpringMVC中去啦
@Controller @Path(ContactsResource.CONTACTS_URL) public class ContactsResource { public static final String CONTACTS_URL = "/contacts"; @Autowired ContactService service; @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Path("data") public Contacts getAll() { return service.getAll(); } @PUT @POST @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Path("data") public Response saveContact(@Context UriInfo uri, Contact contact) throws URISyntaxException { service.save(contact); URI newURI = UriBuilder.fromUri(uri.getPath()).path(contact.getLastName()).build(); return Response.created(newURI).build(); } @GET @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Path("data/{lastName}") public Contact get(@PathParam("lastName") String lastName) { return service.getContact(lastName); } @POST @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.TEXT_HTML) public ModelAndView saveContactForm(@Form Contact contact) throws URISyntaxException { service.save(contact); return viewAll(); } @GET @Produces(MediaType.TEXT_HTML) public ModelAndView viewAll() { // forward to the "contacts" view, with a request attribute named // "contacts" that has all of the existing contacts return new ModelAndView("contacts", "contacts", service.getAll()); } }