springboot 系统学习 1 -- 集成jersey

最近在学习springboot, 故写下这些文章来记录我的学习历程,本人菜鸟一只,如果有错误的地方,希望大家积极指正,

这篇文章主要是讲 springboot 如何继承jersey, 来时先mvc思想,当然 springboot 是支持controller 来写实现接口的设计,使用@RequestMapping, @RestController 这些注解

但是我还是觉得jersey 看起来更加优雅,然后易于维护和阅读

首先需要搭建一个spingboot项目, 项目地址

build 文件

subprojects {
    apply plugin: 'java'
    apply plugin: 'maven'

    group 'com.smaug'
    version '1.0-SNAPSHOT'

    targetCompatibility = 1.8
    sourceCompatibility = 1.8

    [compileJava, compileTestJava].each() {
        it.options.encoding = "UTF-8"
    }

    repositories {
        mavenLocal()
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        mavenCentral()
    }


    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    artifacts {
        archives sourcesJar
    }
    configurations.all {
        resolutionStrategy.cacheChangingModulesFor 1, 'minutes'
    }

    dependencies {
        compile('org.springframework.boot:spring-boot-starter:1.5.7.RELEASE') {
            exclude(module: 'spring-boot-starter-logging')
        }

        compile('org.slf4j:slf4j-api:1.7.25')
        compile('org.apache.logging.log4j:log4j-slf4j-impl:2.8.2')
        compile('org.apache.logging.log4j:log4j-core:2.8.2')
        compile('org.apache.logging.log4j:log4j-web:2.8.2')

        compile "org.springframework.boot:spring-boot-starter-jersey:1.5.1.RELEASE"
        compile 'mysql:mysql-connector-java:6.0.6'
        compile 'org.mybatis.generator:mybatis-generator-core:1.3.5'
        compile 'org.projectlombok:lombok:1.16.8'
        compile 'org.springframework:spring-core:4.2.5.RELEASE'
        compile 'org.springframework:spring-context-support:4.2.5.RELEASE'
        compile 'org.springframework:spring-beans:4.2.5.RELEASE'
        compile 'org.springframework:spring-oxm:4.2.5.RELEASE'
        compile "org.springframework:spring-jms:4.2.5.RELEASE"
        compile 'org.springframework.retry:spring-retry:1.1.2.RELEASE'
        compile 'org.springframework:spring-context:4.2.5.RELEASE'

        compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.9'
        compile 'com.alibaba:fastjson:1.2.38'

        //testCompile "junit:junit:4.12"
        testCompile 'org.springframework:spring-test:5.0.0.RELEASE'
        testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.7.RELEASE'
    }
}

创建一个smaug_cloud_api module 并写一个interface

package smaug.cloud.api.interfaces;

import smaug.cloud.api.vos.test.TestResponse;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * Created by Allen on 17/10/10.
 */
@Path("test")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({"application/json;charset=UTF-8", "text/xml;charset=UTF-8"})
public interface TestService {

    @POST
    @Path("test")
    String test();

    @POST
    @Path("getUser")
    TestResponse getUser();
}

新建module smaug_cloud_service, 创建TestServiceImpl 用来实现 TestService

package smaug.cloud.provider.impl;


import org.springframework.stereotype.Service;
import smaug.cloud.api.interfaces.TestService;
import smaug.cloud.api.vos.test.TestResponse;
import smaug.cloud.common.utils.jsons.FastJsonUtil;
import smaug.cloud.common.utils.jsons.JsonUtil;

/**
 * Created by Allen on 17/10/10.
 */
@Service("testServiceImpl")
public class TestServiceImpl implements TestService {

    protected JsonUtil jsonUtil = new FastJsonUtil();

    @Override
    public String test() {
        return "test";
    }

    @Override
    public TestResponse getUser() {
        TestResponse response = new TestResponse();
        response.setId(1);
        response.setName("13");
        System.out.println(jsonUtil.toJson(response));
        return response;
    }
}

创建一个JerseyConfig类

package smaug.cloud.config.jerseryConfig;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

/**
 * Created by Allen on 17/10/10.
 */
public class AnnotationJerseyConfig extends ResourceConfig {
    public AnnotationJerseyConfig() {
        register(RequestContextFilter.class);
        packages("smaug.cloud.api.interfaces");
    }
}

在项目启动类里

package smaug.cloud.provider;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import smaug.cloud.config.jerseryConfig.AnnotationJerseyConfig;

/**
 * Created by Allen on 17/10/10.
 */

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class SmaugCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(SmaugCloudApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, AnnotationJerseyConfig.class.getName());
        return registration;
    }

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        jsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        converter.setFastJsonConfig(jsonConfig);

        HttpMessageConverter<?> httpConverter = converter;
        return new HttpMessageConverters(httpConverter);
    }
}

jerseyServlet 方法就是实现 jersey 的bean
此时运行启动即可

猜你喜欢

转载自blog.csdn.net/weixin_39526391/article/details/78235819