Spring Boot与JAX-RS框架Jersey的完美搭配

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}

转载于:https://my.oschina.net/wstone/blog/1580479

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
去几年,REST逐渐成为影响Web框架、Web协议与Web应用设计的重要概念。如果你还不了解REST,那这个简短的介绍将有助你快速掌握REST,此外还可以点击这里了解关于REST的更多信息。 相关厂商内容 高速下载:Adobe Flash Builder 4 简体中文正式版 for Windows 高速下载:Adobe Flash Builder 4 简体中文正式版 for Mac 利用Flex SDK创建易于访问的Adobe AIR应用程序 Adobe和英特尔联手推新服务帮助开发者发行AIR应用 构建更加完善的Adobe AIR应用程序之十大秘诀 相关赞助商 汇集最新RIA技术相关资源,提供Flash开发平台相关工具高速下载,免费获得Adobe软件的产品序列号。 现在有越来越多的公司希望能以简单而又贴合Web架构本身的方式公开Web API,因此REST变得越来越重要也就不足为奇了。使用Ajax进行通信的富浏览器端也在朝这个目标不断迈进。这个架构原则提升了万维网的可伸缩性,无论何种应用都能从该原则中受益无穷。 JAX-RS(JSR 311)指的是Java API for RESTful Web Services,Roy Fielding也参与了JAX-RS的制订,他在自己的博士论文中定义了REST。对于那些想要构建RESTful Web Services的开发者来说,JAX-RS给出了不同于JAX-WS(JSR-224)的另一种解决方案。目前共有4种JAX-RS实现,所有这些实现都支持SpringJersey则是JAX-RS的参考实现,也是本文所用的实现。 如果你使用Spring进行开发,那可能想知道(或者有人曾问过你)Spring MVC与JAX-RS有何异同点?更进一步,如果你手头有一个Spring MVC应用,使用了控制类继承(SimpleFormController等),你可能还意识不到现在的Spring MVC对REST广泛的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值