spring与jersey的集成
来源:互联网 发布:点击按钮执行php代码 编辑:程序博客网 时间:2024/05/19 23:52
pom.xml添加依赖
< dependency>
<groupId > org.glassfish.jersey.containers</ groupId >
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId > jersey-container-servlet </artifactId >
<version > 2.5</ version >
</ dependency>
<!-- Required only when you are using JAX-RS Client -->
< dependency>
< groupId> org.glassfish.jersey.core </groupId >
< artifactId> jersey-client </artifactId >
< version> 2.5 </version >
</ dependency>
<!--与spring集成时添加的关键依赖-->
< dependency>
< groupId> org.glassfish.jersey.ext </groupId >
< artifactId> jersey-spring3 </artifactId >
< version> 2.5 </version >
</ dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.6.RELEASE</version>
<scope>compile</scope>
</dependency> -->
< dependency>
< groupId> commons-logging </groupId >
< artifactId> commons-logging </artifactId >
< version> 1.1 </version >
< exclusions>
< exclusion>
< groupId> javax.servlet </groupId >
< artifactId> servlet- api</ artifactId>
</ exclusion>
</ exclusions>
</ dependency>
< dependency>
< groupId> javax.servlet </groupId >
< artifactId> servlet- api</ artifactId>
< version> 2.5 </version >
</ dependency>
web.xml
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation </param-name >
< param-value> classpath:applicationContext.xml </param-value >
</ context-param>
<servlet >
< servlet-name> Jersey REST Service </servlet-name >
< servlet-class> org.glassfish.jersey.servlet.ServletContainer </servlet-class >
<!--这里只是其中一种暴漏资源的方式-->
< init-param>
< param-name> javax.ws.rs.Application </param-name >
< param-value> com.hpf.restfullservice.spring.Myapplication </param-value >
</ init-param>
< load-on-startup> 1</ load-on-startup >
</servlet >
<servlet-mapping >
< servlet-name> Jersey REST Service </servlet-name >
< url-pattern> /rest/* </url-pattern >
</servlet-mapping >
Myapplication.java
import org.glassfish.jersey.server.ResourceConfig;
importorg.glassfish.jersey.server.spring.scope.RequestContextFilter;
public class Myapplication extends ResourceConfig{
public Myapplication(){
register(RequestContextFilter. class );
register(Hello. class );//注册需要暴漏的资源
}
}
applicationContext.xml
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<!-- 开启注解配置 -->
<context:annotation-config />
<!-- 对指定的包进行组件扫描 -->
<context:component-scan base-package= "com.hpf" />
</ beans>
至此配置就完成了,可以使用提供资源类了:例如:
Hello.java
package com.hpf.restfullservice.spring;
import java.net.URLEncoder;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.CommonProperties;
import org.springframework.beans.factory.annotation.Autowired;
@Path("/hello")
public class Hello {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@Autowired
GreetingService greetingService;
@Path("/")
@GET
@Consumes(MediaType.TEXT_PLAIN+";charset=UTF-8")
@Produces(MediaType.TEXT_PLAIN+";charset=UTF-8")
public String sayHello(String username) throws Exception{
return greetingService.greet("my");
}
}
import java.net.URLEncoder;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.CommonProperties;
import org.springframework.beans.factory.annotation.Autowired;
@Path("/hello")
public class Hello {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@Autowired
GreetingService greetingService;
@Path("/")
@GET
@Consumes(MediaType.TEXT_PLAIN+";charset=UTF-8")
@Produces(MediaType.TEXT_PLAIN+";charset=UTF-8")
public String sayHello(String username) throws Exception{
return greetingService.greet("my");
}
}
0 0
- spring与jersey的集成
- Jersey REST Spring集成
- jersey的简单介绍及与spring-boot的集成使用
- jersey的简单介绍及与spring-boot的集成使用
- jersey和spring集成使用
- jersey和spring集成,不能注入service的问题解决方法
- jersey spring shiro mybatis mysql集成配置
- jersey和spring集成配置使用
- Jersey+Spring+Hibernate集成学习记录
- Jersey与Spring联姻,对JavaBean的支持
- jersey+spring+mybatis+maven框架集成和实现简单的crud
- 94.Spring Boot集成Jersey【从零开始学Spring Boot】
- Spring与iBATIS的集成
- Spring与iBATIS的集成
- Spring与iBATIS的集成
- Spring与iBATIS的集成
- Spring与iBATIS的集成
- Spring与iBATIS的集成
- sizeof,终极无惑(上)
- 在CentOS 6.4中编译安装gcc 4.8.1 + gdb 7.6.1 + Eclipse
- 还是畅通工程
- 关于Umeng错误信息调试
- jsp - 下拉列表框
- spring与jersey的集成
- ACM练级日志:POJ 3740 与Dancing Links
- vector和list区别
- HDU 1862 EXCEL排序 (排序水题)
- 继续畅通工程
- Android学习Scroller(四)——实现拉动后回弹的布局
- 继承、方法重写和多态
- ACM练级日志:POJ 3074 数独与DLX
- servlet容器中使用jersey 翻译官网