Srping-boot-Jersey 例子代码

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-jersey

基于该例子,做了些许扩展和实际应用的案例: class-diagram


Springboot-Jersey.png

微服务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'
}


作者:kexue
链接:http://www.jianshu.com/p/54e22aff2dbe
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值