首页 > 其他分享 >JAX-RS之resteasy跟spring整合

JAX-RS之resteasy跟spring整合

时间:2022-12-08 12:36:30浏览次数:62  
标签:CustomerBo http JAX spring customerBo import resteasy org public


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

1 比如有个接口和实现类

Java代码 ​​ ​


1. public interface
2.
3. String getMsg();
4.
5. }

public interface CustomerBo{

String getMsg();

}



  实现类


  

1. public class CustomerBoImpl implements
2.
3. public
4.
5. return "RESTEasy + Spring example";
6.
7. }


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 ,当然这个类要单例


 

​​

JAX-RS之resteasy跟spring整合_path

1. import
2. import
3. import
4.
5. public class SpringApplicationContext implements
6.
7. private static
8.
9. // Private constructor prevents instantiation from other classes
10. private
11.
12. @Override
13. public void
14. throws
15. appContext = applicationContext;
16.
17. }
18.
19. public static
20. return
21. }
22.
23. }

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>



使用:




    1. import
    2. import
    3. import
    4. import
    5. import
    6.
    7. @Path("/customer")
    8. public class
    9.
    10. CustomerBo customerBo;
    11.
    12. @GET
    13. @Path("/print")
    14. public
    15.
    16. "customerBo");
    17.
    18. String result = customerBo.getMsg();
    19.
    20. return Response.status(200).entity(result).build();
    21.
    22. }
    23.
    24. }


    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>


    标签:CustomerBo,http,JAX,spring,customerBo,import,resteasy,org,public
    From: https://blog.51cto.com/u_14230175/5920916

    相关文章

    • Spring Cloud之统一配置中心Config配置手工刷新
      实际上,配置文件也是随着应用场景变化的,如果服务已经启动并在使用过程中时需要修改其中某些配置,微服务可以直接识别并使用么?下面以SpringCloud之统一配置中心Config初......
    • flex3+blazeds+spring+hibernate整合小结
         近来flex盛行,因此这两天也借了本书看了两天,发觉作为非页面设计人员,flex还是很好的,flexbuilder很好用,拖拉就有很COOL的界面了,而且flex总的来说基本东西不难学,有编程基础......
    • 解析Spring中的ResponseBody和RequestBody
      ​​https://www.cnkirito.moe/2017/08/30/%E8%A7%A3%E6%9E%90Spring%E4%B8%AD%E7%9A%84ResponseBody%E5%92%8CRequestBody/​​​spring,restful,前后......
    • spring security 4.1两个不错功能介绍
      一转眼,springsecurity已经发布4.1了,查看了下新特性,有两个比较值得关注:1)可以在pathvariable形式的URL中进行保护了 比如有个方法:[code=......
    • java springboot 大文件分片上传处理
      ​  1 背景用户本地有一份txt或者csv文件,无论是从业务数据库导出、还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工、挖掘和共创应用的时候,首先要将本地......
    • Spring 集成提供的各种通道适配器和消息传递网关
      本节介绍Spring集成提供的各种通道适配器和消息传递网关,以支持与外部系统的基于消息的通信。每个系统,从AMQP到Zookeeper,都有自己的集成要求,本节将介绍它们。端点快速参考......
    • Spring支撑ApplicationEvent
      Spring集成提供了对入站和出站的支持,由底层Spring框架定义。有关Spring对事件和侦听器的支持的更多信息,请参阅 Spring参考手册​。​​ApplicationEvents​​您需要......
    • 黑马程序员2022新版SSM框架Spring+SpringMVC+Maven高级+SpringBoot+MyBatisPlus企业实
      Spring为什么要学Spring?1.专业角度:简化开发,降低企业级开发的复杂性框架整合,高效整合其他计算,提高企业级应用开发与运行效率2.学什么?简化开发IOCAOP事务......
    • 三、继续进行——Asp.net ajax的主要控件
       一、前言打开工具箱的Ajaxextensions可以看到五个ajax控件,在vs2008中它们已经集成到了.netframerwork3.5中,如果是之前版本的vs则需要自己去下载文件来安装。这五个控件......
    • 使用SpringBoot时出现了找不到测试类的情况或There are test failures
      出现场景:在使用SpringBoot做单元测试时在Maven编译或打包项目时具体bug描述:Therearetestfailures或者找不到测试类解决方案:首先去运行控制台看causeby后面的......