jersey的简单介绍及与spring-boot的集成使用

来源:互联网 发布:阿里云深圳节点怎么样 编辑:程序博客网 时间:2024/06/06 03:34
转载:http://blog.csdn.net/zhangjq520/article/details/54314256

一、什么是jersey?

Jersey RESTful 框架是开源的RESTful框架, 实现了JAX-RS (JSR 311 & JSR 339) 规范。

它扩展了JAX-RS 参考实现, 提供了更多的特性和工具, 可以进一步地简化 RESTful service 和 client 开发。与Struts类似,它同样可以和hibernate,

spring框架整合。

Jersey共计有4中处理方式,即:@GET,@POST,@DELETE,@PUT。由于Jersey中文资料较少。想学习的可以通过官网API学习。
jersey常用注解解释:

Annotation作用说明@GET查询请求相当于数据库的查询数据操作@POST插入请求相当于数据库的插入数据操作@PUT更新请求相当于数据库的更新数据操作@DELETE删除请求相当于数据的删除数据操作@Pathuri路径定义资源的访问路径,client通过这个路径访问资源。比如:@Path(“user”)@Produces指定返回MIME格式资源按照那种数据格式返回,可取的值有:MediaType.APPLICATION_XXX。比如:@Produces(MediaType.APPLICATION_XML)@Consumes接受指定的MIME格式只有符合这个参数设置的请求再能访问到这个资源。比如@Consumes(“application/x-www-form-urlencoded”)@PathParamuri路径参数写在方法的参数中,获得请求路径参数。比如:@PathParam(“username”) String userName@QueryParamuri路径请求参数写在方法的参数中,获得请求路径附带的参数。比如:@QueryParam(“desc”) String desc@DefaultValue设置@QueryParam参数的默认值如果@QueryParam没有接收到值,就使用默认值。比如:@DefaultValue(“description”) @QueryParam(“desc”) String desc@FormParamform传递的参数接受form传递过来的参数。比如:@FormParam(“name”) String userName@BeanParam通过Bena的形式传递参数接受client传递的bean类型的参数,同时这个bean可以在属性上配置@FormParam用以解决client的属性名称和bean的属性名称不一致的问题。比如:@BeanParam User user@Context获得一些系统环境信息通过@Context可以获得以下信息:UriInfo、ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse和HttpHeaders等@XmlRootElement将bean转换为xml如果要讲bean以xml或json的格式返回,必须要这个注解。比如:
@XmlRootElement
public class User{…}
@XmlElements  @XmlElement 

二、与spring-boot的使用

pom.xml

[html] view plain copy
print?
  1. <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  2.   xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>spring-boot-Jersey</groupId>  
  5.   <artifactId>spring-boot-Jersey</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>spring-boot-Jersey</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <properties>  
  11.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.     <!– jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 –>  
  13.     <java.version>1.7</java.version>  
  14.   </properties>  
  15.   <!–  
  16.        spring boot 父节点依赖,  
  17.        引入这个之后相关的引入就不需要添加version配置,  
  18.        spring boot会自动选择最合适的版本进行添加。  
  19.      –>  
  20.     <parent>  
  21.        <groupId>org.springframework.boot</groupId>  
  22.        <artifactId>spring-boot-starter-parent</artifactId>  
  23.        <version>1.4.0.RELEASE</version>  
  24.     </parent>  
  25.    
  26.   <dependencies>  
  27.     <dependency>  
  28.       <groupId>junit</groupId>  
  29.       <artifactId>junit</artifactId>  
  30.       <scope>test</scope>  
  31.     </dependency>  
  32.      
  33.     <!– jersey –>  
  34.     <dependency>  
  35.        <groupId>org.springframework.boot</groupId>  
  36.        <artifactId>spring-boot-starter-jersey</artifactId>  
  37.     </dependency>  
  38.      
  39.   </dependencies>  
  40.   <build>  
  41.   <plugins>  
  42.         <plugin>  
  43.             <artifactId>maven-compiler-plugin</artifactId>  
  44.             <configuration>  
  45.             <source>1.7</source>  
  46.             <target>1.7</target>  
  47.             </configuration>  
  48.         </plugin>  
  49.     </plugins>      
  50.     <finalName>spring-boot-Jersey</finalName>  
  51.   </build>  
  52. </project>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>spring-boot-Jersey</groupId>  <artifactId>spring-boot-Jersey</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>spring-boot-Jersey</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->    <java.version>1.7</java.version>  </properties>  <!--       spring boot 父节点依赖,       引入这个之后相关的引入就不需要添加version配置,       spring boot会自动选择最合适的版本进行添加。     -->    <parent>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-parent</artifactId>       <version>1.4.0.RELEASE</version>    </parent>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <scope>test</scope>    </dependency>    <!-- jersey -->    <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-jersey</artifactId>    </dependency>  </dependencies>  <build>  <plugins>        <plugin>            <artifactId>maven-compiler-plugin</artifactId>            <configuration>            <source>1.7</source>            <target>1.7</target>            </configuration>        </plugin>    </plugins>        <finalName>spring-boot-Jersey</finalName>  </build></project>

App.java

[html] view plain copy
print?
  1. package com.zjq;  
  2.   
  3. import org.glassfish.jersey.servlet.ServletContainer;  
  4. import org.glassfish.jersey.servlet.ServletProperties;  
  5. import org.springframework.boot.SpringApplication;  
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  7. import org.springframework.boot.web.servlet.ServletRegistrationBean;  
  8. import org.springframework.context.annotation.Bean;  
  9.   
  10. import com.zjq.config.JerseyConfig;  
  11.   
  12. @SpringBootApplication  
  13. public class App {  
  14.     public static void main(String[] args) {  
  15.         SpringApplication.run(App.class, args);  
  16.     }  
  17.       
  18.     @Bean  
  19.     public ServletRegistrationBean jersetServlet(){  
  20.         ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), ”/jersey/*”);  
  21.         // our rest resources will be available in the path /jersey/*  
  22.         registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());  
  23.         return registration;  
  24.     }  
  25. }  
package com.zjq;import org.glassfish.jersey.servlet.ServletContainer;import org.glassfish.jersey.servlet.ServletProperties;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import com.zjq.config.JerseyConfig;@SpringBootApplicationpublic class App {    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }    @Bean    public ServletRegistrationBean jersetServlet(){        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/jersey/*");        // our rest resources will be available in the path /jersey/*        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());        return registration;    }}

JerseyConfig.java

[java] view plain copy
print?
  1. package com.zjq.config;  
  2.   
  3. import org.glassfish.jersey.server.ResourceConfig;  
  4. import org.glassfish.jersey.server.spring.scope.RequestContextFilter;  
  5.   
  6. public class JerseyConfig extends ResourceConfig{  
  7.     public JerseyConfig() {  
  8.            register(RequestContextFilter.class);  
  9.            //配置restful package.  
  10.            packages(”com.zjq”);  
  11.         }  
  12. }  
package com.zjq.config;import org.glassfish.jersey.server.ResourceConfig;import org.glassfish.jersey.server.spring.scope.RequestContextFilter;public class JerseyConfig extends ResourceConfig{    public JerseyConfig() {           register(RequestContextFilter.class);           //配置restful package.           packages("com.zjq");        }}

JerseyTest.java

[java] view plain copy
print?
  1. package com.zjq;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.ws.rs.GET;  
  7. import javax.ws.rs.Path;  
  8. import javax.ws.rs.Produces;  
  9. import javax.ws.rs.core.MediaType;  
  10.   
  11. import org.springframework.stereotype.Component;  
  12.   
  13. @Path(“/”)  
  14. @Component  
  15. public class JerseyTest {  
  16.     @GET  
  17.     @Produces(MediaType.APPLICATION_JSON)  
  18.     @Path(“/hello”)  
  19.     public Map<String,Object> hello() {  
  20.        Map<String,Object> map = new HashMap<String,Object>();  
  21.        map.put(”code”,“1”);  
  22.        map.put(”codeMsg”“success”);  
  23.        return map;  
  24.     }  
  25. }  
package com.zjq;import java.util.HashMap;import java.util.Map;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import org.springframework.stereotype.Component;@Path("/")@Componentpublic class JerseyTest {    @GET    @Produces(MediaType.APPLICATION_JSON)    @Path("/hello")    public Map<String,Object> hello() {       Map<String,Object> map = new HashMap<String,Object>();       map.put("code","1");       map.put("codeMsg", "success");       return map;    }}

访问:http://localhost:8080/jersey/hello

结果:

{"codeMsg":"success","code":"1"}

阅读全文
0 0