Spring MVC4配置Swagger2

  1. pom.xml

	io.springfox
	springfox-swagger2
	2.7.0


	io.springfox
	springfox-swagger-ui
	2.7.0


	com.fasterxml.jackson.core
	jackson-core
	2.8.7


	com.fasterxml.jackson.core
	jackson-databind
	2.8.7


	com.fasterxml.jackson.core
	jackson-annotations
	2.8.7

  1. spring-mvc.xml





		
			
				
					
						text/plain;charset=UTF-8
						text/html;charset=UTF-8
						application/json;charset=UTF-8
						application/x-www-form-urlencoded;charset=UTF-8
					
				
			


			
				
					
						application/json;charset=UTF-8
					
				
			
		
	
  1. Swagger2Config.java
@Configuration
@EnableSwagger2
public class Swagger2Config{

    public Swagger2Config() {
        System.out.println("Swagger2Config.Swagger2Config");
    }

    @Bean
    public Docket createRestApi() {
        System.out.println("Swagger2Config.createRestApi");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ecs.swagger2.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户管理系统在线API查询")
                .description("描述用户管理系统在线API查询的接口")
                .termsOfServiceUrl("https://blog.csdn.net/mxj588love")
                .version("1.0")
                .build();
    }
}
  1. Swagger2Controller.java
@Api(tags = "用户接口")
@RestController
@RequestMapping
public class Swagger2Controller {

    @ApiOperation(value = "获取服务器信息", notes = "获取服务器默认配置信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getServerInfo")
    public String getServerInfo(HttpServletRequest request, ModelMap modelMap) {
        modelMap.addAttribute("RemoteAddr", request.getRemoteAddr());
        modelMap.addAttribute("RemoteHost", request.getRemoteHost());
        modelMap.addAttribute("RemotePort", request.getRemotePort());
        modelMap.addAttribute("RemoteUser", request.getRemoteUser());
        return modelMap.toString();
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/getUserList")
    @ResponseBody
    public List getEquipTypeInfo() {
        List list = Arrays.asList("zhangsan", "lisi", "wangwu");
        return list;
    }

    @ApiOperation(value = "添加用户", notes = "添加一个用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "用户地址", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "city", value = "所属城市", dataType = "String", paramType = "query")
    })

    @PostMapping("/putUser")
    @ResponseBody
    public String putUser(ModelMap modelMap) {
        modelMap.addAttribute("title", "添加用户");
        modelMap.addAttribute("success", true);
        return modelMap.toString();
    }
}
  1. 汉化,在resources目录中新增一个目录META-INF/resources, 在此目录中新建swagger-ui.html, 内容如下:



    
    NEW Swagger UI
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    






  1. 遇到的问题
    Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway.
    The base url is the root of where all the swagger resources are served. For e.g. if the api is available at
    http://example.org/api/v2/api-docs then the base url is http://example.org/api/.
    Please enter the location manually:

这个是因为没有配置json转换器, 在上面的2中

你可能感兴趣的:(Spring)