Commit c67c41d5 by 徐奇

INIT

parent a8083faf
......@@ -33,6 +33,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -49,6 +53,23 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
......
# 第一个SpringBoot 应用
## bean定义
* @RestController
* @Controller
* @Service
* @Repository
* @Component
## bean的注入
* @Autowired
* @Resource(name = "defaultProductService")
* 还有setter注入、构造函数注入,一般不使用
## swagger文档
* 增加pom
* 增加注释
* 增加配置
* 访问地址:http://localhost:8080/swagger-ui.html
## Spring MVC
* thymeleaf
## lombok
* idea 插件安装
......@@ -2,7 +2,9 @@ package cn.freemud.firstboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@SpringBootApplication
public class FirstBootApplication {
......
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.config;
import com.google.common.base.Predicates;
import io.swagger.annotations.Api;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
//@Profile({"dev", "test"})
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(Predicates.and(RequestHandlerSelectors.basePackage("cn.freemud"),
RequestHandlerSelectors.withClassAnnotation(Api.class)))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API")
.description(" APIs")
.termsOfServiceUrl("http://www.freemud.cn/")
.version("1.0")
.build();
}
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.dao;
import cn.freemud.firstboot.vo.ProductCreateReq;
import cn.freemud.firstboot.vo.ProductResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
/**
* @author qi.xu 2018/11/7 11:15
*/
@Slf4j
@Repository
public class ProductDao {
private static final String PRODUCT_NAME = "产品名称";
private static final String NEWID = "NEWID";
/**
*
* @param id
* @return
*/
public ProductResp getById(String id) {
ProductResp productResp = new ProductResp();
productResp.setId(id);
productResp.setProductName(id + PRODUCT_NAME);
return productResp;
}
/**
*
* @param req
* @return
*/
public ProductResp create(ProductCreateReq req) {
ProductResp productResp = new ProductResp();
productResp.setId(NEWID);
productResp.setProductName(req.getProductName());
return productResp;
}
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.service;
import cn.freemud.firstboot.vo.ProductCreateReq;
import cn.freemud.firstboot.vo.ProductResp;
/**
* @author qi.xu 2018/11/7 11:15
*/
public interface ProductService {
ProductResp getById(String id);
ProductResp create(ProductCreateReq req);
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.service.impl;
import cn.freemud.firstboot.dao.ProductDao;
import cn.freemud.firstboot.service.ProductService;
import cn.freemud.firstboot.vo.ProductCreateReq;
import cn.freemud.firstboot.vo.ProductResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author qi.xu 2018/11/7 11:16
*/
@Service
@Slf4j
public class DefaultProductService implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public ProductResp getById(String id) {
log.info("id = {}", id);
return productDao.getById(id);
}
@Override
public ProductResp create(ProductCreateReq req) {
return productDao.create(req);
}
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author qi.xu 2018/11/7 11:18
*/
@Data
@ApiModel(description = "产品创建请求")
public class ProductCreateReq {
@ApiModelProperty(value = "产品名称")
private String productName;
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author qi.xu 2018/11/7 11:18
*/
@Data
@ApiModel(description = "产品返回")
public class ProductResp {
@ApiModelProperty(value = "产品ID")
private String id;
@ApiModelProperty(value = "产品名称")
private String productName;
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.web;
import cn.freemud.firstboot.service.ProductService;
import cn.freemud.firstboot.vo.ProductCreateReq;
import cn.freemud.firstboot.vo.ProductResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Api
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
@Autowired
private ProductService productService;
@Resource(name = "defaultProductService")
private ProductService defaultProductService;
@ApiOperation(value = "根据ID获取产品", notes = "根据ID获取产品")
@ApiImplicitParam(value = "请求", required = true, dataType = "String", name = "id")
@GetMapping("/getById")
public ProductResp getById(String id) {
return productService.getById(id);
}
@ApiOperation(value = "根据ID获取产品", notes = "根据ID获取产品")
@ApiImplicitParam(value = "请求", required = true, dataType = "String", name = "id")
@RequestMapping(value = "/getById2", method = RequestMethod.GET)
public ProductResp getById2(@RequestParam String id) {
return defaultProductService.getById(id);
}
@ApiOperation(value = "创建产品", notes = "创建产品")
@ApiImplicitParam(value = "请求", required = true, dataType = "ProductCreateReq", name = "req")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ProductResp create(@RequestBody ProductCreateReq req) {
return productService.create(req);
}
}
/*
* Copyright (c) 2018 www.freemud.cn Inc. All rights reserved.
* 注意:本内容仅限于上海非码科技内部传阅,禁止外泄以及用于其他的商业目
*/
package cn.freemud.firstboot.web;
import cn.freemud.firstboot.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Api
@Controller
@RequestMapping("/page")
@Slf4j
public class ProductPageController {
@Autowired
private ProductService productService;
@Resource(name = "defaultProductService")
private ProductService defaultProductService;
@ApiOperation(value = "创建产品", notes = "创建产品")
@ApiImplicitParam(value = "请求", required = true, dataType = "String", name = "id")
@GetMapping("/create")
public String kafkaTest(Model model, String id) {
model.addAttribute("id", id);
return "product";
}
}
spring.application.name=first-spring-boot-service
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>产品</title>
</head>
<body>
<H1>创建产品</H1>
产品id: <span th:text="${id}">ID</span>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment