通用技术 基于 spring boot 搭建 REST 服务

bauul · 2017年11月01日 · 最后由 zher 回复于 2017年11月03日 · 2515 次阅读

开发环境搭建

  1. java8 install and env set up
  2. mvn set up
  3. intellij idea set up
  4. lombok plugin install

阅读基础

  1. 掌握 java
  2. 了解常用的设计模式
  3. 了解 spring,mybatis
  4. 了解 maven 知识
  5. 了解 RESTful 知识

基于的服务

  1. spring boot
  2. mybatis
  3. swagger2
  4. lombok

pom 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.carl</groupId>
    <artifactId>mobile-test-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mobile-test-service</name>
    <description>Mobile Test Service</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator-docs</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-data-mongodb</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>6.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

说明一下,这里的 orcale 的 jar 包是我通过 mvn install 的方法单独放到我本地的仓库中去的

mvn install:install-file -Dfile=C:/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=6.0 -Dpackaging=jar -DgeneratePom=true 

application.properties

### Database for Oracle ###
spring.datasource.url=jdbc:oracle:thin:@192.168.2.7:1521:orcl
spring.datasource.username=zyolap
spring.datasource.password=zyolap123
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

### Mybatis ###
#显示执行的SQL命令
logging.level.com.carl.mobiletestservice=DEBUG

源码

入口类

package com.carl.mobiletestservice;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.carl.mobiletestservice.mapper")
public class MobileTestServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(MobileTestServiceApplication.class, args);
    }
}

bean

package com.carl.mobiletestservice.beans;

import lombok.Data;

@Data
public class Event {
    private String  eventid;
}

mapper

package com.carl.mobiletestservice.mapper;

import com.carl.mobiletestservice.beans.Event;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

@Component(value = "eventMapper")
public interface EventMapper {

    @Select("SELECT * FROM dw_event_fact_c where rownum = 1")
    Event getOne();

    @Select("SELECT * FROM dw_event_fact_c where ${key} = '" + "${value}' AND rownum = 1")
    Event getOneByKeyValue(@Param("key") String key, @Param("value") String value);
}

controller

package com.carl.mobiletestservice.controller;

import com.carl.mobiletestservice.beans.Event;
import com.carl.mobiletestservice.mapper.EventMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "event")
@Slf4j
public class EventController {

    @Autowired
    private EventMapper eventMapper;

    @ApiOperation(value = "获取最新的一条记录")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Event getEvent() {
        log.debug("getEvent:" + eventMapper.getOne().toString());
        return eventMapper.getOne();
    }

    @ApiOperation(value = "根据key和value获取最新的一条记录")
    @RequestMapping(value = "/key/{key}/value/{value}", method = RequestMethod.GET)
    public Event getEvent(@PathVariable String key, @PathVariable String value) {
        log.debug("getEvent:" + eventMapper.getOneByKeyValue(key, value).toString());
        return eventMapper.getOneByKeyValue(key, value);
    }
}

swagger

package com.carl.mobiletestservice;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.carl.mobiletestservice.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("移动端测试服务")
                .contact(new Contact("clark", "https://testerhome.com/carl", "bauul@126.com"))
                .version("1.0")
                .build();
    }

}

URL 访问

Swagger2 访问

打包发布

mvn package

jar 包运行

java -jar ***.jar
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 2 条回复 时间 点赞

从草稿而来😈
用来做测试服务

违心点个赞

bauul 数据库封装,有好的思路建议吗 中提及了此贴 11月07日 21:18
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册