springboot 0709

xiaoxiao2021-02-28  81

去年6月进入新的公司,正好碰上项目重构java,采用的技术是ssm,现在打算是springboot重构一下,记录一下springboot的学习步骤

首先一些简单的demo我就不列了,直接从项目结构来看 项目目前分为2个moudle, 但是以后肯定会继续补充,目前项目分为 smaug_api 和smaug_provider两个模块, api负责接口的定义,provider负责接口的实现。整个项目采用gradle 来构建

来看一下项目基础架构

好了,现在看一下项目的gradle文件大概是个什么样子哈哈

subprojects { apply plugin: 'java' apply plugin: 'maven' group 'gold_smaug' version '1.0-SNAPSHOT' targetCompatibility = 1.8 sourceCompatibility = 1.8 [compileJava, compileTestJava].each() { //it.options.compilerArgs += ["-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:-options"] 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 } sourceSets { main { java { srcDirs = ['src/main/java', 'src/main/thrift-java'] } resources { srcDirs = ['src/main/resources'] } } test { java { srcDirs = ['src/test/java', 'src/test/thrift'] } resources { srcDirs = ['src/test/resources'] } } } configurations.all { resolutionStrategy.cacheChangingModulesFor 1, 'minutes' } dependencies { compile('org.springframework.boot:spring-boot-starter:1.5.2.RELEASE') { exclude(module: 'spring-boot-starter-logging') } compile 'com.alibaba:dubbo:2.8.5-SNAPSHOT' compile "org.springframework.boot:spring-boot-starter-jersey:1.5.1.RELEASE" 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 "org.apache.logging.log4j:log4j-api:2.8" compile "org.apache.logging.log4j:log4j-core:2.8" compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.9' testCompile "junit:junit:4.12" testCompile 'com.googlecode.thread-weaver:threadweaver:0.2' } }

现在看一下service_api的 gradle

def artifactId = "smaug_api" dependencies { compile 'javax.validation:validation-api:1.1.0.Final' compile 'javax.ws.rs:javax.ws.rs-api:2.0' compile 'javax.annotation:javax.annotation-api:1.2' compile 'org.jboss.resteasy:resteasy-client:3.0.9.Final' compile 'org.jboss.resteasy:resteasy-jaxrs:3.0.9.Final' compile 'org.jboss.resteasy:resteasy-netty:3.0.14.Final' compile 'org.jboss.resteasy:resteasy-multipart-provider:3.0.14.Final' compile 'org.codehaus.jackson:jackson-core-asl:1.9.13' compile 'com.esotericsoftware.kryo:kryo:2.24.0' compile 'de.javakaffee:kryo-serializers:0.37' compile 'org.apache.thrift:libthrift:0.9.3' }

and smaug_provider的 gradle

apply plugin: 'application' def artifactId = "smaug_provider" def env = System.getProperty("env") ?: "test" sourceSets { main { resources { srcDirs = ["src/main/resources", "src/main/profile/$env"] } } } dependencies { compile(project(":smaug_api")) compile('org.springframework.boot:spring-boot-starter:1.5.2.RELEASE') { exclude(module: 'spring-boot-starter-logging') } compile 'org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE' compile "org.springframework.boot:spring-boot-starter-jersey" compile 'org.jboss.resteasy:resteasy-client:3.0.9.Final' compile 'org.jboss.resteasy:resteasy-jaxrs:3.0.9.Final' compile 'org.jboss.resteasy:resteasy-netty:3.0.14.Final' compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.9' compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.8.9' } processResources { exclude { "**/*.*" } } task pack(type: Copy, dependsOn: [clean, installDist]) { sourceSets.main.resources.srcDirs.each { from it into "$buildDir/install/$artifactId/lib/resources" } } mainClassName = 'smaug.service.provider.starts.TestStartApplication'

然后我打算用 spring-boot-starter-jersey 来取代mvc 构建 接口层和实现层的分离 细心的小伙伴已经发现了gradle 文件里有dependencies

compile "org.springframework.boot:spring-boot-starter-jersey"

下面开始放大招了哈 首先我定义了一个 TestService

package smaug.service.api; /** * Created by naonao on 17/7/9. */ import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType; import smaug.service.vo.UserEntity; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @Path("test") @Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) @Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) public interface TestService { @POST @Path("ping") UserEntity ping(); }

然后我又实现了这个接口

package smaug.service.provider.impl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import smaug.service.api.TestService; import smaug.service.vo.UserEntity; /** * Created by naonao on 17/7/9. */ @Service("testService") public class TestServiceImpl implements TestService { private Logger logger = LoggerFactory.getLogger(TestServiceImpl.class); @Override public UserEntity ping() { UserEntity user = new UserEntity(); user.setUserId(1); user.setName("闹闹"); return user; } }

启动文件如下

package smaug.service.provider.starts; 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.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import smaug.service.provider.config.JerseyConfig; /** * Created by naonao on 17/7/9. */ @SpringBootApplication @EnableAutoConfiguration public class TestStartApplication { public static void main(String[] args) { SpringApplication.run(TestStartApplication.class, args); } // @Bean // public ServletRegistrationBean jerseyServlet() { // ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*"); // // our rest resources will be available in the path /rest/* // registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName()); // return registration; // } }

运行项目, 然后赶紧试试刚刚那个接口如何(满怀期待)

{ "timestamp": 1499691013884, "status": 404, "error": "Not Found", "message": "No message available", "path": "/smaug/test/ping" }

然而 404 ,是因为接口并没有被启动类扫描到 然后我们该咋办捏 —– 修改启动类

package smaug.service.provider.starts; 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.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import smaug.service.provider.config.JerseyConfig; /** * Created by naonao on 17/7/9. */ @SpringBootApplication @ComponentScan(value = {"smaug.service"}) @EnableAutoConfiguration public class TestStartApplication { public static void main(String[] args) { SpringApplication.run(TestStartApplication.class, args); } @Bean public ServletRegistrationBean jerseyServlet() { ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*"); // our rest resources will be available in the path /rest/* registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName()); return registration; } }

然后这次我可以满心欢喜的去看啦

看一下区别, 就是把api包添加到启动类下了 其中有个config 就是把smaug.api导入

package smaug.service.provider.config; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.web.filter.RequestContextFilter; /** * Created by naonao on 17/7/9. */ public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(RequestContextFilter.class); //配置restful package. packages("smaug.service.api"); } } { "userId": 1, "name": "闹闹" }
转载请注明原文地址: https://www.6miu.com/read-41471.html

最新回复(0)