jersey的简单介绍及与spring-boot的集成使用
2017-01-10 15:26
846 查看
一、什么是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 | 删除请求 | 相当于数据的删除数据操作 |
@Path | uri路径 | 定义资源的访问路径,client通过这个路径访问资源。比如:@Path("user") |
@Produces | 指定返回MIME格式 | 资源按照那种数据格式返回,可取的值有:MediaType.APPLICATION_XXX。比如:@Produces(MediaType.APPLICATION_XML) |
@Consumes | 接受指定的MIME格式 | 只有符合这个参数设置的请求再能访问到这个资源。比如@Consumes("application/x-www-form-urlencoded") |
@PathParam | uri路径参数 | 写在方法的参数中,获得请求路径参数。比如:@PathParam("username") String userName |
@QueryParam | uri路径请求参数 | 写在方法的参数中,获得请求路径附带的参数。比如:@QueryParam("desc") String desc |
@DefaultValue | 设置@QueryParam参数的默认值 | 如果@QueryParam没有接收到值,就使用默认值。比如:@DefaultValue("description") @QueryParam("desc") String desc |
@FormParam | form传递的参数 | 接受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<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
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; @SpringBootApplication public 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
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
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("/") @Component public 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"}
相关文章推荐
- jersey的简单介绍及与spring-boot的集成使用
- SpringBoot | 第二章:lombok 介绍及简单使用
- Redis+Spring集成使用简单介绍
- SpringBoot | 第六章:常用注解介绍及简单使用
- SpringBoot | 第二章:lombok介绍及简单使用
- Spring Boot相关介绍及简单使用
- Spring Boot(1)集成MyBatis的简单使用
- spring boot 、springMVC环境集成百度ueditor富文本编辑器,使用七牛云存储图片
- Spring 配置log4j和简单介绍Log4J的使用
- Mongodb安装、简单使用及与spring的集成
- spring-boot集成activeMQ(一)-使用默认的ActiveMQ
- springboot简单介绍
- Spring Boot集成Dubbo简单示例
- #Spring#spring小程序,简单介绍spring的使用
- 简单介绍Log4J的使用和Spring 配置log4j
- jersey和spring集成配置使用
- spring boot 之@JsonView 简单介绍
- 使用buffalo集成spring写的一个简单的登录子模块
- MQ原理、使用场景、IBM WebSphere MQ介绍及spring集成配置
- Spring 配置log4j和简单介绍Log4J的使用