日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

編輯推薦:

本文來自于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知識可以關注小編!

分享到:
標簽:Spring Boot
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定