提交 d5ebc1c7 authored 作者: ljj's avatar ljj

新增swgger

上级 ef231142
...@@ -17,6 +17,20 @@ ...@@ -17,6 +17,20 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
......
...@@ -2,8 +2,12 @@ package com.example.jpademo; ...@@ -2,8 +2,12 @@ package com.example.jpademo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication @SpringBootApplication
@EnableSwagger2
@EnableWebMvc
public class JpaDemoApplication { public class JpaDemoApplication {
public static void main(String[] args) { public static void main(String[] args) {
...@@ -11,3 +15,4 @@ public class JpaDemoApplication { ...@@ -11,3 +15,4 @@ public class JpaDemoApplication {
} }
} }
package com.example.jpademo.conf;
import com.google.common.collect.Sets;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
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;
import java.io.File;
/**
* fusion-platform.
*
* @author : Matrix [xhyrzldf@gmail.com]
* 19-1-10 .
*/
@Configuration
public class Swagger2Config extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.produces(Sets.newHashSet("application/json"))
.consumes(Sets.newHashSet("application/json"))
.protocols(Sets.newHashSet("http", "https"))
.apiInfo(apiInfo())
.forCodeGeneration(true)
.useDefaultResponseMessages(true)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build()
;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 文档标题
.title("XX系统接口文档")
// 文档描述
.description("XX系统接口文档与测试页面")
.termsOfServiceUrl("http://localhost:8080")
.version("v1")
.build();
}
/**
* 我们使用的 @Configuration 继承了WebMvcConfigurationSupport,导致默认的Swagger静态资源被覆盖,而缺失了配置。
* @param registry registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
...@@ -2,6 +2,9 @@ package com.example.jpademo.controller; ...@@ -2,6 +2,9 @@ package com.example.jpademo.controller;
import com.example.jpademo.pojo.Student; import com.example.jpademo.pojo.Student;
import com.example.jpademo.service.StudentService; import com.example.jpademo.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -14,16 +17,19 @@ import java.util.List; ...@@ -14,16 +17,19 @@ import java.util.List;
*/ */
@RestController @RestController
@RequestMapping("/model") @RequestMapping("/model")
@Api(tags = "学生模块", description = "学生模块的接口信息")
public class StudentController { public class StudentController {
@Autowired @Autowired
private StudentService studentService; private StudentService studentService;
@GetMapping("/student") @GetMapping("/student")
public ResponseEntity<Student> findOne(@RequestParam Integer id) { @ApiOperation(value = "根据学生id查找学生的接口")
public ResponseEntity<Student> findOne(@RequestParam @ApiParam(value = "学生id", example = "10") Integer id) {
return ResponseEntity.ok(studentService.findById(id)); return ResponseEntity.ok(studentService.findById(id));
} }
@ApiOperation(value = "保存学生的接口")
@PostMapping("/student") @PostMapping("/student")
public Student saveOne(@RequestBody Student student) { public Student saveOne(@RequestBody Student student) {
return studentService.saveOne(student); return studentService.saveOne(student);
......
package com.example.jpademo.pojo; package com.example.jpademo.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -14,16 +16,21 @@ import javax.persistence.*; ...@@ -14,16 +16,21 @@ import javax.persistence.*;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Entity @Entity
@ApiModel("人员信息")
public class Student { public class Student {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "主键id", example = "1")
private Integer id; private Integer id;
@ApiModelProperty(value = "姓名", example = "张三")
private String name; private String name;
@ApiModelProperty(value = "年龄", example = "15")
private Integer age; private Integer age;
@ApiModelProperty(value = "外键", example = "1")
private Integer unionId; private Integer unionId;
@Transient @Transient
......
server.port=8079 server.port=8078
# mysql jdbc # mysql jdbc
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论