Spring Boot整合Swagger2构建RESTful API

pom.xml



	4.0.0

	com.example
	Spring-Boot-Swagger2
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.11.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.7
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
		    io.springfox
		    springfox-swagger2
		    2.6.1
		
		
		
		    io.springfox
		    springfox-swagger-ui
		    2.6.1
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	




swagger配置

package com.example.demo.config;

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 SwaggerConfig {
	
	@Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                .title("系统RESTful API文档")
                .contact(new Contact("mrbird", "https://mrbird.cc", "[email protected]"))
                .version("1.0")
                .build();
    }
}

controller

package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.domain.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "用户Controller")
@Controller
@RequestMapping("user")
public class UserController {

	@ApiIgnore
	@GetMapping("hello")
	public @ResponseBody String hello() {
		return "hello";
	}

	@ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息")
	@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path")
	@GetMapping("/{id}")
	public @ResponseBody User getUserById(@PathVariable(value = "id") Long id) {
		User user = new User();
		user.setId(id);
		user.setName("mrbird");
		user.setAge(25);
		return user;
	}

	@ApiOperation(value = "获取用户列表", notes = "获取用户列表")
	@GetMapping("/list")
	public @ResponseBody List getUserList() {
		List list = new ArrayList<>();
		User user1 = new User();
		user1.setId(1l);
		user1.setName("mrbird");
		user1.setAge(25);
		list.add(user1);
		User user2 = new User();
		user2.setId(2l);
		user2.setName("scott");
		user2.setAge(29);
		list.add(user2);
		return list;
	}

	@ApiOperation(value = "新增用户", notes = "根据用户实体创建用户")
	@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
	@PostMapping("/add")
	public @ResponseBody Map addUser(@RequestBody User user) {
		Map map = new HashMap<>();
		map.put("result", "success");
		return map;
	}

	@ApiOperation(value = "删除用户", notes = "根据用户id删除用户")
	@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path")
	@DeleteMapping("/{id}")
	public @ResponseBody Map deleteUser(@PathVariable(value = "id") Long id) {
		Map map = new HashMap<>();
		map.put("result", "success");
		return map;
	}

	@ApiOperation(value = "更新用户", notes = "根据用户id更新用户")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User") })
	@PutMapping("/{id}")
	public @ResponseBody Map updateUser(@PathVariable(value = "id") Long id, @RequestBody User user) {
		Map map = new HashMap<>();
		map.put("result", "success");
		return map;
	}

}

Spring Boot整合Swagger2构建RESTful API_第1张图片Spring Boot整合Swagger2构建RESTful API_第2张图片

你可能感兴趣的:(spring,boot)