Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Model Center   Code  
»áÔ±   
   
 
     
   
 ¶©ÔÄ
  ¾èÖú
Spring BootÓëJAX-RS¿ò¼ÜJerseyµÄÍêÃÀ´îÅä
 
  5559  次浏览      29
 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·½Ê½ÊDzÉÓÃɨÃè°üµÄ·½Ê½ÅúÁ¿×¢²á

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}

 
   
5559 ´Îä¯ÀÀ       29
Ïà¹ØÎÄÕÂ

Java΢·þÎñÐÂÉú´úÖ®Nacos
ÉîÈëÀí½âJavaÖеÄÈÝÆ÷
JavaÈÝÆ÷Ïê½â
Java´úÂëÖÊÁ¿¼ì²é¹¤¾ß¼°Ê¹Óð¸Àý
Ïà¹ØÎĵµ

JavaÐÔÄÜÓÅ»¯
Spring¿ò¼Ü
SSM¿ò¼Ü¼òµ¥¼òÉÜ
´ÓÁ㿪ʼѧjava±à³Ì¾­µä
Ïà¹Ø¿Î³Ì

¸ßÐÔÄÜJava±à³ÌÓëϵͳÐÔÄÜÓÅ»¯
JavaEE¼Ü¹¹¡¢ Éè¼ÆÄ£Ê½¼°ÐÔÄܵ÷ÓÅ
Java±à³Ì»ù´¡µ½Ó¦Óÿª·¢
JAVAÐéÄâ»úÔ­ÀíÆÊÎö