https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-jersey
基于该例子,做了些许扩展和实际应用的案例: class-diagram
微服务Spring boot入口:
package com.wdxxl.jersey;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@SpringBootApplication
public class SampleJerseyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
new SampleJerseyApplication()
.configure(new SpringApplicationBuilder(SampleJerseyApplication.class))
.run(args);
}
}
配置文件注册 通过register 方便其他类在@Inject时候调用到
package com.wdxxl.jersey;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
import com.wdxxl.jersey.filter.RequestFilter;
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(Endpoint.class);
register(ReverseEndpoint.class);
//filter
register(RequestFilter.class);
}
}
Restful 访问入口1,也注入了service方法,可以在UML图中体现
package com.wdxxl.jersey;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.stereotype.Component;
@Component
@Path("/hello")
public class Endpoint {
@Inject
private MessageService service;
@GET
public String message() {
return "Hello " + this.service.message();
}
}
消息输出 并且有读取项目路径下的配置文件.
@Value("${message:World}") 如果配置文件中没有message的key 则输出默认值 World
package com.wdxxl.jersey;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:wdxxl.properties")
public class MessageService {
@Value("${message:World}")
private String msg;
public String message() {
return this.msg;
}
}
Restful 访问入口2
package com.wdxxl.jersey;
import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import org.springframework.stereotype.Component;
@Component
@Path("/reverse")
public class ReverseEndpoint {
@GET
public String reverse(@QueryParam("input") @NotNull String input) {
return new StringBuilder(input).reverse().toString();
}
}
过滤器 为Restful 访问添加Authrization要求
package com.wdxxl.jersey.filter;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
@Provider
public class RequestFilter implements ContainerRequestFilter{
@Context
private HttpServletRequest httpRequest;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String authorizationHeader = httpRequest.getHeader("Authorization");
System.out.println(authorizationHeader + "test");
//Demo of Authorization
if(!"wdxxl".equals(authorizationHeader)){
requestContext.abortWith(
Response.status(Response.Status.UNAUTHORIZED)
.entity("Header is not authorized!")
.build());
}
}
}
Gradle.build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'mySpringBootJersey'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-jersey")
compile("org.springframework.boot:spring-boot-starter-tomcat")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}