您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
   
 
     
   
 订阅
  捐助
Spring Boot与JAX-RS框架Jersey的完美搭配
 
   次浏览      
 2019-4-18
 
编辑推荐:
本文来自于oschina,本文主要介绍了一个在 spring boot 项目添加Jeresy框架的详细过程,希望对大家能有所帮助。

Jeresy是一个轻量级的JAX-RS框架

添加Jeresy 2.x的依赖

compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.26'
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.26'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.26'

build.gradle文件内容:

buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenLocal()
maven{ url "http://SVN:8081/nexus/content/groups/public"}
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

repositories {
mavenLocal()
maven{ url "http://SVN:8081/nexus/content/groups/public"}
mavenCentral()
jcenter()
}


dependencies {
compile('org.springframework.boot:spring-boot-configuration-processor')
compile('org.springframework.boot:spring-boot-starter-web')

compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.26'
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.26'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.26'


testCompile('org.springframework.boot:spring-boot-starter-test')
}

创建一个 spring boot 项目

在IDE里一路next

Spring Boot启动APP

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure
.SpringBootApplication;

@SpringBootApplication // @wjw 是Sprnig Boot项目的核心注解,主要目的是开启自动配置.
public class JerseyApplication {

public static void main(String[] args) {
SpringApplication.run(JerseyApplication.class, args);
}

}

注册jersey servlet

这和原来在 web.xml 配置的是一样的,设置 Mapping,设置 init 初始化参数,对应的 servlet class name .

所有的rest/*请求都将被 ServletContainer jersey servlet 容器接管.

package com.example.demo;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.web.servlet.
ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.
Configuration;

@Configuration
public class ConfigBean {

@Bean
public ServletRegistrationBean jerseyServlet() {
//手动注册servlet
ServletRegistrationBean registrationBean = new
ServletRegistrationBean(new ServletContainer(),
"/rest/*");
registrationBean.addInitParameter
(ServletProperties.JAXRS_APPLICATION_CLASS
,JerseyResourceConfig.class.getName());


return registrationBean;
}
}

创建jersey Resources

packages方式是采用扫描包的方式批量注册

register 是单个注册

package com.example.demo;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.web.filter
.RequestContextFilter;

public class JerseyResourceConfig extends ResourceConfig
{

public JerseyResourceConfig() {
/*
* Servlet Filter that exposes the request to
the current thread, through both org.springframework.context.i18n.LocaleContextHolder
and RequestContextHolder. To be registered as
filter in web.xml.<br/>
Alternatively, Spring's org.springframework.web.context
.request.RequestContextListener and Spring's org.springframework.web.servlet.DispatcherServlet
also expose the same request context to the
current thread.<br/>
This filter is mainly for use with third-party
servlets, e.g. the JSF FacesServlet. Within
Spring's own web support, DispatcherServlet's
processing is perfectly sufficient.<br/>
*/
register(RequestContextFilter.class);

// 加载资源文件,这里直接扫描com.example.
demo.controller下的所有api
packages("com.example.demo.controller");
//register(HelloController.class);
//@wjw_note: 这种是注册单个的 JAX-RS component!
}
}

创建jersey Controller,使用 JAX-RS 规范的注解进行设置即可

package com.example.demo.controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.example.demo.User;

@Path("/user/")
public class HelloController {

@Path("{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public User hello(@PathParam("id") Long id) {
User user = new User();
user.setID(id);
user.setUserName("mvc.");

return user;
}
}

启动Spring Boot程序

默认端口号是:8080

在浏览器里测试

输入:http://127.0.0.1:8080/rest//user/123678

返回: {"userName": "mvc.","id": 123678}

 
   
次浏览       
相关文章

Java微服务新生代之Nacos
深入理解Java中的容器
Java容器详解
Java代码质量检查工具及使用案例
相关文档

Java性能优化
Spring框架
SSM框架简单简绍
从零开始学java编程经典
相关课程

高性能Java编程与系统性能优化
JavaEE架构、 设计模式及性能调优
Java编程基础到应用开发
JAVA虚拟机原理剖析