乱人伦 国语对白海角社区,五月激情丁香婷婷综合中文字幕,欧美伊人婷婷久久五月综合,亚洲精品无amm毛片,亚洲男人第一无码AV网站,国产日韩欧美丝袜一区二区,亚洲一区精品在线观看

Spring Boot与JAX-AG九游会

Spring Boot与JAX

2026-01-16 04:46:06投稿人:彩31彩票APP下載(賀州)有限公司圍觀1948163 評(píng)論

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

編輯推薦:

本文來(lái)自于oschina ,本文主要介紹了一個(gè)在 spring boot 項(xiàng)目添加Jeresy框架的詳細(xì)過(guò)程 ,希望對(duì)大家能有所幫助 。

Jeresy是一個(gè)輕量級(jí)的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文件內(nèi)容:

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')}

創(chuàng)建一個(gè) spring boot 項(xiàng)目

在IDE里一路next

Spring Boot啟動(dòng)APP

package com.example.demo; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // @wjw 是Sprnig Boot項(xiàng)目的核心注解,主要目的是開(kāi)啟自動(dòng)配置.public class JerseyApplication {  public static void main(String[] args) { SpringApplication.run(JerseyApplication.class, args);} } 

注冊(cè)jersey servlet

這和原來(lái)在 web.xml 配置的是一樣的,設(shè)置 Mapping,設(shè)置 init 初始化參數(shù),對(duì)應(yīng)的 servlet class name .

所有的rest/*請(qǐng)求都將被 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;@Configurationpublic class ConfigBean { @Beanpublic ServletRegistrationBean jerseyServlet() { //手動(dòng)注冊(cè)servletServletRegistrationBean registrationBean = new ServletRegistrationBean(new ServletContainer(), "/rest/*");registrationBean.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS,JerseyResourceConfig.class.getName()); return registrationBean;}}

創(chuàng)建jersey Resources

packages方式是采用掃描包的方式批量注冊(cè)

register 是單個(gè)注冊(cè)

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.
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.
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.
*/register(RequestContextFilter.class);// 加載資源文件,這里直接掃描com.example.demo.controller下的所有api packages("com.example.demo.controller"); //register(HelloController.class); //@wjw_note: 這種是注冊(cè)單個(gè)的 JAX-RS component!}}

創(chuàng)建jersey Controller,使用 JAX-RS 規(guī)范的注解進(jìn)行設(shè)置即可

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;}}

啟動(dòng)Spring Boot程序

默認(rèn)端口號(hào)是:8080

在瀏覽器里測(cè)試

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

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



因?yàn)閮?nèi)容實(shí)在是太多了,小編在此就不做過(guò)多的介紹了,想了解更多的Java知識(shí)可以關(guān)注小編 !

展開(kāi)閱讀全文

投稿時(shí)間