RESTEasy integrates with Spring 3.0.x. We are interested in other forms of Spring integration, so please help contribute.
41.1. Basic Integration
For Maven users, you must use the resteasy-spring artifact. Otherwise, the jar is available in the downloaded distribution.
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>whatever version you are using</version>
</dependency>
RESTeasy comes with its own Spring ContextLoaderListener that registers a RESTeasy specific BeanPostProcessor that processes JAX-RS annotations when a bean is created by a BeanFactory. What does this mean? RESTeasy will automatically scan for @Provider and JAX-RS resource annotations on your bean class and register them as JAX-RS resources.
Here is what you have to do with your web.xml file
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
The SpringContextLoaderListener must be declared after ResteasyBootstrap as it uses ServletContext attributes initialized by it.
If you do not use a Spring ContextLoaderListener to create your bean factories, then you can manually register the RESTeasy BeanFactoryPostProcessor by allocating an instance of org.jboss.resteasy.plugins.spring.SpringBeanProcessor. You can obtain instances of a ResteasyProviderFactory and Registry from the ServletContext attributes org.jboss.resteasy.spi.ResteasyProviderFactory and org.jboss.resteasy.spi.Registry. (Really the string FQN of these classes). There is also a org.jboss.resteasy.plugins.spring.SpringBeanProcessorServletAware, that will automatically inject references to the Registry and ResteasyProviderFactory from the Servlet Context. (that is, if you have used RestasyBootstrap to bootstrap Resteasy).
Our Spring integration supports both singletons and the "prototype" scope. RESTEasy handles injecting @Context references. Constructor injection is not supported though. Also, with the "prototype" scope, RESTEasy will inject any @*Param annotated fields or setters before the request is dispatched.
NOTE: You can only use auto-proxied beans with our base Spring integration. You will have undesirable affects if you are doing handcoded proxying with Spring, i.e., with ProxyFactoryBean. If you are using auto-proxied beans, you will be ok.
41.2. Spring MVC Integration
RESTEasy can also integrate with the Spring DispatcherServlet. The advantages of using this are that you have a simpler web.xml file, you can dispatch to either Spring controllers or Resteasy from under the same base URL, and finally, the most important, you can use Spring ModelAndView objects as return arguments from @GET resource methods. Setup requires you using the Spring DispatcherServlet in your web.xml file, as well as importing the springmvc-resteasy.xml file into your base Spring beans xml file. Here's an example web.xml file:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
</web-app>
Then within your main Spring beans xml, import the springmvc-resteasy.xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- Import basic SpringMVC Resteasy integration -->
<import resource="classpath:springmvc-resteasy.xml"/>
....
You can specify resteasy configuration options by overriding the resteasy.deployment bean which is an instance of org.jboss.resteasy.spi.ResteasyDeployment. Here's an example of adding media type suffix mappings as well as enabling the Resteasy asynchronous job service.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- Import basic SpringMVC Resteasy integration -->
<import resource="classpath:springmvc-resteasy.xml" />
<!-- override the bean definition for deployment -->
<bean id="resteasy.deployment" class="org.jboss.resteasy.spi.ResteasyDeployment" init-method="start" destroy-method="stop">
<property name="asyncJobServiceEnabled" value="true"/>
<property name="mediaTypeMappings">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
...
分享到:
相关推荐
**标题解析:** "resteasy-spring-poc-swagger:resteasy-spring-...通过这个 POC,开发者可以学习如何在 Spring 环境下有效地使用 Resteasy 创建和管理 RESTful 服务,并结合 Swagger 提供清晰的 API 文档和测试环境。
【标题】"Resteasy_Spring_GAE_sample"是一个示例项目,它展示了如何在Google App Engine(GAE)上结合使用Resteasy和Spring框架。这是一个关键的开发实践,因为GAE是一个流行的云平台,用于部署Java应用程序,而...
4. **集成性**:RestEasy可以轻松地与各种Java EE容器(如Tomcat、Jetty、GlassFish等)以及Spring框架集成。 5. **拦截器和过滤器**:开发者可以通过实现特定接口或使用注解定义拦截器和过滤器,实现请求和响应的...
9. **文档生成**:Resteasy可以与Swagger等工具结合,自动生成REST API的文档,方便开发者理解和使用服务。 10. **测试工具**:为了简化测试,Resteasy提供了一套测试框架,允许开发者在不部署到服务器的情况下测试...
在实际项目中,可能还需要结合其他工具,如Spring框架进行依赖注入,或者使用Eclipse、IntelliJ IDEA等IDE进行开发。`resteasy-jaxrs-all-beta1`可能包含了RestEasy的完整组件集,包括客户端库和各种模块,这对于...
RESTEasy与Spring框架结合,可以方便地进行依赖注入,提高代码的可测试性和可维护性。 环境配置方面,至少需要JDK 1.6或更高版本,Apache Tomcat 6.0服务器(或其他兼容的容器),以及Spring 3框架。开发工具推荐...
**JAX-RS与Spring结合使用** 1. **集成方式**: 通常通过Spring的Servlet容器如Tomcat或Jetty来部署JAX-RS应用,Spring负责应用的上下文管理和依赖注入,JAX-RS处理REST接口。 2. **Spring REST**: 使用Spring MVC...
restful 基于resteasy实现的restful webservice。和spring mvc结合起来使用。部署在tomcat和jboss中都可以。jboss中包含了部分restful的基础jar。
RESTEasy可以与多种框架集成,如Spring和CDI。它支持多种内容类型,包括JSON和XML,以及JAX-RS的注解,如@Path、@GET、@POST等,使得开发者能够轻松地在Java类和HTTP方法之间建立映射。 结合这三个技术,可以构建一...
5. **resteasy-spring-boot-master**:RestEasy是一个JAX-RS实现,与Spring Boot结合可能用于构建RESTful API的服务端应用。 6. **demo**:可能包含各种不同场景下的Spring应用示例,比如AOP、IoC、MVC等。 7. **...
5. **Spring Boot与Spring Cloud Contract**:结合Spring Boot的快速开发特性,Spring Cloud Contract可以用于生成服务器端的WebService接口以及客户端的测试桩。这种方式强调了消费者驱动的契约,确保服务提供者和...
在实际开发中,开发者通常会结合使用多个框架,例如,Spring Boot结合Spring MVC和MyBatis,构建一个完整的后端系统;而前端可能采用Angular或React,配合Webpack等构建工具进行构建。 描述中提到的博客链接指向...
这个项目通过结合这些技术,为全栈开发者提供了一个实践环境,学习如何整合前端与后端,构建完整的Web应用。通过学习和操作这个项目,开发者可以深入理解Spring MVC的控制器、服务和DAO层的运作方式,掌握Thymeleaf...
在线考试系统是现代教育技术与计算机科学相结合的产物,它为教师、学生提供了一个便捷的平台,可以在网络上进行各种类型的考试。这个基于Java的在线考试系统,利用了Java的强大功能和灵活性,以及其在网络应用开发中...
本文将深入探讨REST架构与Web2.0的结合,以及如何利用Java技术实现这一概念。 **REST架构的核心概念** 1. **资源(Resource)**: 在REST中,一切皆为资源。资源通过唯一的URI(Uniform Resource Identifier)进行...
4. **集成Spring框架**:RESTEasy可以很好地与Spring框架集成,提供依赖注入和AOP(面向切面编程)等功能。 5. **错误处理**:通过`@ExceptionMapper`注解,可以自定义异常处理逻辑,优雅地处理服务端错误。 6. **...
常见的JAX-RS实现有Apache CXF、Jersey和RESTEasy等。这些实现提供了处理HTTP请求、映射URL、处理异常、数据绑定等功能,简化了REST服务的开发。 2. **Jersey**:作为JAX-RS的实现之一,Jersey提供了全面的工具和库...
- **Spring Boot**:虽然不是专门的REST框架,但Spring Boot提供了一种简便的方式来构建REST服务。 选择开发工具时需要考虑项目的具体需求和个人喜好。例如,如果项目需要高度定制化的REST服务,那么Jersey可能是更...
Java与XML的结合广泛应用于配置文件(如Spring框架的bean配置)、数据交换(如SOAP消息的生成与解析)等。例如,JAXB(Java Architecture for XML Binding)允许Java对象与XML文档之间进行自动转换,简化了数据交换...