JAX-RS之resteasy跟spring整合

其实,在JAX-RS标准下,jboss的resteasy跟spring结合的话,无非是如何去取得
spring中的bean而已.两个方法,例子如下

1 比如有个接口和实现类

public interface CustomerBo{

String getMsg();

}


实现类

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
[quote]
@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();

}

[/quote]

第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);
}

}

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();

}

}


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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值