去年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相关推荐

  1. 继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错? springboot 两种方式稳定解决跨域问题

    继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错???springboot 两种方式稳定解决跨域问题! 之前我写了一篇文章,来解决CORS报错 ...

  2. Dockerfile springboot项目拿走即用,将yml配置文件从外部挂入容器

    Dockerfile 将springboot项目jar包打成镜像,并将yml配置文件外挂. # 以一个镜像为基础,在其上进行定制.就像我们之前运行了一个 nginx 镜像的容器,再进行修改一样,基础镜 ...

  3. SpringBoot部署脚本,拿走即用!

    一个可以直接拿来使用的shell脚本,适用于springboot项目 #!/bin/bash # 这里可替换为你自己的执行程序,其他代码无需更改,绝对路径相对路径均可. # 若使用jenkins等工具 ...

  4. SpringBoot项目使用nacos,kotlin使用nacos,java项目使用nacos,gradle项目使用nacos,maven项目使用nacos

    SpringBoot项目使用nacos kotlin demo见Gitte 一.引入依赖 提示:这里推荐使用2.2.3版本,springboot与nacos的依赖需要版本相同,否则会报错. maven ...

  5. springboot整合swagger2之最佳实践

    来源:https://blog.lqdev.cn/2018/07/21/springboot/chapter-ten/ Swagger是一款RESTful接口的文档在线自动生成.功能测试功能框架. 一 ...

  6. SpringBoot中实现quartz定时任务

    Quartz整合到SpringBoot(持久化到数据库) 背景 最近完成了一个小的后台管理系统的权限部分,想着要扩充点东西,并且刚好就完成了一个自动疫情填报系统,但是使用的定时任务是静态的,非常不利于 ...

  7. Springboot 利用AOP编程实现切面日志

    前言 踏入Springboot这个坑,你就别想再跳出来.这个自动配置确实是非常地舒服,帮助我们减少了很多的工作.使得编写业务代码的时间占比相对更大.那么这里就讲一下面向切面的日志收集.笔者使用lomb ...

  8. 【Springboot】日志

    springBoot日志 1.目前市面上的日志框架: 日志门面 (日志的抽象层):                JCL(Jakarta Commons Logging)                ...

  9. 【springboot】配置

    配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoot自 ...

最新文章

  1. Node.js开发WEB项目后端接口API,基于mysql5.7数据库(小试牛刀)
  2. windows 2003服务器不断向外发包解决方法 php程序
  3. 第十六届智能车竞赛开源云台设计
  4. 微信编辑照片到底该不该增加滤镜功能?
  5. C++为什么要内存对齐
  6. 【转】新浪微博手机客户端刷新都是手动刷新或者下拉刷新,为什么不设计成自动刷新?...
  7. CF1119G. Get Ready for the Battle
  8. 一起来玩树莓派--解决复制文件时出现error opening file... permission denied问题
  9. html 空链接 href=#与href=javascript:void(0)的区别
  10. 软件测试用例最简单最常见的模板和案例(QQ登陆,手机号,126邮箱)
  11. python五笔输入法_centos下安装五笔输入法的教程
  12. CET-4 week9 阅读 写译
  13. 图解CSS3 Flexbox属性
  14. python练习-prat1
  15. [解決]如何利用EXCEL依照固定欄位取出值
  16. 1bit和1byte_带宽单位是位(bit)网速单位是字节(Byte)1字节等于8位
  17. [问题/解决]Could not chdir to home directory /home/zwj: Permission denied
  18. 基于Nano Pi NEO4开发板的AS项目开发
  19. 解决SQL Server报错:229、262、5123
  20. 起步HarmonyOS生态的入门学习路线及资源

热门文章

  1. 如何成为一名优秀的网络工程师?
  2. FC金手指代码大全·持续更新-亲测可用-FC 经典游戏完整可用的金手指大全---持续更新,偶尔玩玩经典回味无穷,小时候不能通关的现在通通通关一遍
  3. 盈通rx580游戏高手 bios_RX 5700 XT D6 游戏高手测评:女装大佬重捶出击!
  4. Unity3D使用经验总结 优点篇
  5. 解决笔记本windows11充电后,屏幕亮度忽明忽暗的问题
  6. 如何最用最懒的方式获取百度地图的行政区边界坐标范围
  7. C语言中双下划线__的作用
  8. 红石外汇|每日汇评:黄金多头在美国CPI指数之前仍保持希望
  9. 计算机的音乐设置方法,让电脑开机和关机音乐更个性的设置方法(图文)
  10. 电脑打不开计算机设备管理,如何解决 设备管理器打不开的问题 设备管理器打不开怎么解决...