jersey 简介
JAX-RS是JAVA EE6 引入的一个新技术。 JAX-RS即Java API for RESTful Web Services,是一个Java 编程语言的应用程序接口,支持按照表述性状态转移(REST)架构风格创建Web服务。JAX-RS使用了Java SE5引入的Java注解来简化Web服务的客户端和服务端的开发和部署。
RESTful Web 服务简介
REST 在 2000 年由 Roy Fielding 在博士论文中提出,他是 HTTP 规范 1.0 和 1.1 版的首席作者之一。
REST 中最重要的概念是资源(resources),使用全球 ID(通常使用 URI)标识。客户端应用程序使用 HTTP 方法(GET/ POST/ PUT/ DELETE)操作资源或资源集。RESTful Web 服务是使用 HTTP 和 REST 原理实现的 Web 服务。通常,RESTful Web 服务应该定义以下方面:
- Web 服务的基/根 URI,比如 http://host/<appcontext>/resources。
- 支持 MIME 类型的响应数据,包括 JSON/XML/ATOM 等等。
- 服务支持的操作集合(例如 POST、GET、PUT 或 DELETE)
下面开始整合
1、创建一个maven工程
创建过程自行百度
2、pom.xml配置:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> </dependencies>
3、创建JerseyConfig类:
import org.glassfish.jersey.server.ResourceConfig; import org.springframework.web.filter.RequestContextFilter; public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(RequestContextFilter.class); //配置restful package. packages("com.services"); } }
4、创建RestResource类:
该类可通过rest请求访问
package com.services; 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 RestResource { @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; } }
5、创建app启动类
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; @SpringBootApplication public class App { @Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*"); // our rest resources will be available in the path /rest/* registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName()); return registration; } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
6、右击app运行访问
http://127.0.0.1:8080/rest/hello
相关推荐
jersey整合spring示例,各种jar包报错,搞了俩小时。
jersey加spring加mybatis整合,支持json转换和事物管理
spring-boot-jersey, Jersey自动配置支持 Spring Boot 这里代码现在是 Spring Boot 1.2.0的一部分。 如果你想用Jersey的话。Originaly这里项目为 Spring Boot 应用程序提供Jersey自动配置。 反馈是非常欢迎的,特别...
jersey加入spring支持 jersey加入spring支持
spring boot + mybatis + jersey + mysql 整合 可以看我的博客理解搭建过程
java应用程序实现jetty 集成jersey 和spring
com.sun.jersey.spi.spring.container.servlet.SpringServlet需要此包的支持,使用spring3.1.1开发web service project时,用于添加Spring框架支持,使用IOC容器接管所有对象实例。解决在web service project中使用...
jersey-spring4-2.26.jar 最新版本spring4-2.26,最新版本spring4-2.26
Jersey-spring3-2.4.1.jar
一个Jersey+Spring框架的示例。
基于Java的微服务实践,单体微服务创建、Docker部署、Dubbo微服务实践等.zip ...spring-boot-jersey Spring Boot集成Jersey spring-boot-swagger Spring Boot集成Swagger2 spring-cloud-demo Spring Cloud示例
代码来自互联网,经过整理测试可用;大家注意里面的说明文档
Spring4整合Jersey2.9的入门案例,采用Maven构建
Jersey Spring4 Freemarker HIbernate整合搭建测试
spring boot中文文档,从安装到部署。 I. Spring Boot文件 1.关于文档 2.获得帮助 3.第一步 4.使用Spring Boot 5.了解Spring Boot功能 6.转向生产 7.高级主题 II。入门 8.介绍Spring Boot ...
Jersey+Spring:解决Jersey单实例问题
spring-jersey 整合的所有的包
搭建springboot项目要用到的springboot的一些jar包(spring-boot,spring-boot-actuator,spring-boot-actuator-autoconfigure,spring-boot-autoconfigure,spring-boot-configuration-processor,spring-boot-...
NULL 博文链接:https://annan211.iteye.com/blog/2148419
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...