JAX-RS之resteasy跟spring整合
来源:互联网 发布:mac 触摸板 鼠标 消失 编辑:程序博客网 时间:2024/06/07 05:22
其实,在JAX-RS标准下,jboss的resteasy跟spring结合的话,无非是如何去取得
spring中的bean而已.两个方法,例子如下
1 比如有个接口和实现类
实现类
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
</bean>
</beans>
之后用WebApplicationContextUtils + ServletContext
@Path("/customer")
public class PrintService {
CustomerBo customerBo;
@GET
@Path("/print")
public Response printMessage(@Context ServletContext servletContext) {
//get Spring application context
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
customerBo= ctx.getBean("customerBo",CustomerBo.class);
String result = customerBo.getMsg();
return Response.status(200).entity(result).build();
}
第2种方法,自己写类,实现ApplicationContextAware ,当然这个类要单例
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<bean class="com.mkyong.context.SpringApplicationContext"></bean>
<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
</bean>
</beans>
使用:
web.xml整合
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.PrintService</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
spring中的bean而已.两个方法,例子如下
1 比如有个接口和实现类
- public interface CustomerBo{
- String getMsg();
- }
public interface CustomerBo{ String getMsg(); }
实现类
- public class CustomerBoImpl implements CustomerBo {
- public String getMsg() {
- return "RESTEasy + Spring example";
- }
public class CustomerBoImpl implements CustomerBo { public String getMsg() { return "RESTEasy + Spring example"; }
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
</bean>
</beans>
之后用WebApplicationContextUtils + ServletContext
引用
@Path("/customer")
public class PrintService {
CustomerBo customerBo;
@GET
@Path("/print")
public Response printMessage(@Context ServletContext servletContext) {
//get Spring application context
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
customerBo= ctx.getBean("customerBo",CustomerBo.class);
String result = customerBo.getMsg();
return Response.status(200).entity(result).build();
}
第2种方法,自己写类,实现ApplicationContextAware ,当然这个类要单例
- import org.springframework.beans.BeansException;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- public class SpringApplicationContext implements ApplicationContextAware {
- private static ApplicationContext appContext;
- // Private constructor prevents instantiation from other classes
- private SpringApplicationContext() {}
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- appContext = applicationContext;
- }
- public static Object getBean(String beanName) {
- return appContext.getBean(beanName);
- }
- }
import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware; public class SpringApplicationContext implements ApplicationContextAware { private static ApplicationContext appContext; // Private constructor prevents instantiation from other classes private SpringApplicationContext() {} @Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {appContext = applicationContext; } public static Object getBean(String beanName) {return appContext.getBean(beanName);} }
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<bean class="com.mkyong.context.SpringApplicationContext"></bean>
<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
</bean>
</beans>
使用:
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.core.Response;
- import com.mkyong.context.SpringApplicationContext;
- import com.mkyong.customer.CustomerBo;
- @Path("/customer")
- public class PrintService {
- CustomerBo customerBo;
- @GET
- @Path("/print")
- public Response printMessage() {
- customerBo = (CustomerBo) SpringApplicationContext.getBean("customerBo");
- String result = customerBo.getMsg();
- return Response.status(200).entity(result).build();
- }
- }
import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.core.Response;import com.mkyong.context.SpringApplicationContext;import com.mkyong.customer.CustomerBo; @Path("/customer")public class PrintService { CustomerBo customerBo; @GET@Path("/print")public Response printMessage() { customerBo = (CustomerBo) SpringApplicationContext.getBean("customerBo"); String result = customerBo.getMsg(); return Response.status(200).entity(result).build(); } }
web.xml整合
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.PrintService</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
- JAX-RS之resteasy跟spring整合
- JAX-RS和spring整合
- JAX-RS 和 和 Spring 整合开发
- webservice jax-rs与spring整合
- JAX-RS 和 Spring 整合开发
- Spring Boot 集成 resteasy篇 — jax-rs初步介绍和spring boot集成
- RESTEasy:JAX-RS restful webservices 示例
- Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍
- 框架 day64 WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- Exception handling in JAX-RS RESTEasy with ExceptionMapper
- 比较各JAX-RS实现:Jersey,Restlet,CXF,RESTEasy
- REST,JAX-RS与Jersey,CXF,RESTEasy之间的关系
- cxf jax-rs spring client
- REST:JAX-RS 与 Spring
- JAX-RS之上传文件
- JAX-RS之Jersey入门
- spring boot 整合 resteasy+fastjson
- Davinci内核编译常用命令
- 字符串函数源代码分析与比较
- 【转】用JS实现弹出模式对话框,实现页面传值
- JAX-RS之与XML打交道
- JAX-RS之jackson去处理json
- JAX-RS之resteasy跟spring整合
- 找出文件中每小时最大利用率值
- 合并排序
- 几个重要的TCP/IP选项解析(Java Socket)
- poj 2352 Star
- 用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台)
- 浪潮实习8.6
- tslib1.0移植
- 双缓冲技术绘图