jersey的简单介绍及与spring-boot的集成使用
来源:互联网 发布:阿里云深圳节点怎么样 编辑:程序博客网 时间:2024/06/06 03:34
转载:http://blog.csdn.net/zhangjq520/article/details/54314256
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的格式返回,必须要这个注解。比如:
App.java
JerseyConfig.java
JerseyTest.java
访问:http://localhost:8080/jersey/hello
一、什么是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
- <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>
<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;
- }
- }
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
- 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”);
- }
- }
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;
- }
- }
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
- jersey的简单介绍及与spring-boot的集成使用
- jersey的简单介绍及与spring-boot的集成使用
- spring与jersey的集成
- Spring Boot(1)集成MyBatis的简单使用
- 使用spring-boot集成dubbo的日志
- jersey和spring集成使用
- 94.Spring Boot集成Jersey【从零开始学Spring Boot】
- Quartz的使用及与spring集成使用
- jersey+spring+mybatis+maven框架集成和实现简单的crud
- guava缓存的使用及与spring的集成
- Spring Boot项目与Docker集成完成打包,打镜像及推送至镜像库的功能
- Spring Boot整合集成StringRedisTemplate的简单记录
- 最简单的spring boot集成mybatis配置
- Spring Boot 的介绍
- CXF与Spring的集成图文介绍
- spring boot test的简单使用
- Spring Boot的Servlet简单使用
- Spring Boot的listener简单使用
- 布局配合动画的使用 动态改变布局的高度
- 剑指Offer_面试题28_字符串的排列
- ubuntu 下caffe框架内一个训练流程小记
- 安卓usb转串口通讯
- js作用域和作用域链心得
- jersey的简单介绍及与spring-boot的集成使用
- java.lang.NoClassDefFoundError:com.ibm.ws.management.configservice.TypeRegistry
- java获取日期时间
- 获取HTML元素3个方法:document.getElementsByName()、document.getElementById()、document.getElementsByTagName()
- j2ME/j2SE/j2EE与JDK/JRE/JVM
- JavaWeb学习总结(十六)——JSP中的九个内置对象
- C#解析Json(包含Model中实体类型字段)
- 大质数检测&&分解质因子 poj1811 Prime Test
- Android开发中遇到的各类问题汇总