à悟空学院:https://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/A6ZagYTi
Spring Cloud视频:http://t.cn/A6ZagxSR
SpringBoot Shiro视频:http://t.cn/A6Zag7IV
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/A6Zad1OH
SpringSecurity5.0视频:http://t.cn/A6ZadMBe
Sharding-JDBC分库分表实战:http://t.cn/A6ZarrqS
分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr
看到群里有人在问如何集成jersey,博主在别的项目也用过jersey,在这里就给大家简单介绍下spring boot中怎么使用jersey。本章大纲如下:
(2)在pom.xml中添加相关依赖;
(3)编写启动文件和注册ServletContainer;
(4)编写Jersey config文件;
(5)编写一个测试文件;
(6)运行测试;
(1)新建工程spring-boot-jersey;
新建一个工程,取名为spring-boot-jersey。
(2)在pom.xml中添加相关依赖;
在pom.xml文件中添加相关的依赖,主要是jersey的依赖,具体如下:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kfit</groupId>
<artifactId>spring-boot-jersey</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<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.8</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>
</project>
(3)编写启动文件和注册ServletContainer;
编写一个App.java启动文件,在此代码中使用ServletRegistrationBean注册ServletContainer,具体代码如下:
package com.kfit;
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.kfit.config.jersey.JerseyConfig;
/**
*
* @author Angel --守护天使
* @version v.0.1
* @date 2016年11月6日
*/
@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);
}
}
(4)编写Jersey config文件;
编写jersey的配置文件,主要是指定扫描的包packages("com.kfit.rest");具体代码如下:
package com.kfit.config.jersey;
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.kfit.rest");
}
}
(5)编写一个测试文件;
编写一个测试文件,这个有jersey基础的就很简单了,使用@Path进行指定访问路径,使用@GET标注是get请求,使用@Produces指定的返回的数据类型。具体代码如下:
package com.kfit.rest;
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;
}
}
(6)运行测试;
剩下的就是运行App.java进行测试了,访问测试地址:
http://127.0.0.1:8080/rest/hello ,好了这就是最基本的一个例子,jersey还是有很多地方需要研究的,剩下的就靠你自己了。
à悟空学院:https://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/A6ZagYTi
Spring Cloud视频:http://t.cn/A6ZagxSR
SpringBoot Shiro视频:http://t.cn/A6Zag7IV
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/A6Zad1OH
SpringSecurity5.0视频:http://t.cn/A6ZadMBe
Sharding-JDBC分库分表实战:http://t.cn/A6ZarrqS
分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr
相关推荐
从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf从零开始学Spring Boot.pdf...
Spring Boot 学习笔记,包括底层实现原理及代码实战,非常齐全,助你快速打通 Spring Boot 的各个环节。 1. Spring boot helloworld 2. Spring boot 返回 json 数据 3. Spring boot 使用其他 json 转换框架 4. ...
从零开始学Spring Boot,没有积分的...(35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】 http://412887952-qq-com.iteye.com/blog/2294942 更多查看博客:http://412887952-qq-com.iteye.com/
Pro Spring Boot is your authoritative hands-on practical guide for increasing your Spring Framework-based enterprise Java and cloud application productivity while decreasing development time using the...
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2294952
Spring Boot 学习示例 Spring Boot 2.0 Mysql 5.6 JDK 1.8 Maven license Spring Boot 使用的各种示例,以最简单、最实用为标准,此开源项目中的每个示例都以最小依赖,最简单为标准,帮助初学者快速掌握 Spring ...
2018.02.Spring Boot 2.0 Cookbook 2nd
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2295146
从零开始学Spring Boot。是学习spring Boot的起步资料!!
This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow ...
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2293846
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2292739
Apress.Beginning.Spring.Boot.2.pdfApress.Beginning.Spring.Boot.2.pdfApress.Beginning.Spring.Boot.2.pdfApress.Beginning.Spring.Boot.2.pdfApress.Beginning.Spring.Boot.2.pdf
赠送jar包:spring-boot-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-autoconfigure-2.3.12.RELEASE-sources.jar; 赠送...
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2305992
Packt.Spring.Boot.2.0.Cookbook.2nd.Edition. Packt.Spring.Boot.2.0.Cookbook.2nd.Edition.
Pro Spring Boot is your authoritative hands-on practical guide for increasing your Spring Framework-based enterprise Java and cloud application productivity while decreasing development time using the...
赠送jar包:spring-boot-configuration-processor-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-configuration-processor-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-...