日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 0709

發布時間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 0709 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

去年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.8sourceCompatibility = 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 itinto "$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);@Overridepublic 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);}@Beanpublic 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": "鬧鬧" }

總結

以上是生活随笔為你收集整理的springboot 0709的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。