Spring MVC 4 Swager 2を配置

8526 ワード

  • 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
    
    
  • スプリング-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
    					
    				
    			
    		
    	
    
  • Swagger 2ちゃんねるfig.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();
        }
    }
    
  • Swagger 2ちゃんねるtroller.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();
        }
    }
    
  • 漢化、resourceディレクトリにMETA-INF/resourceを追加し、このディレクトリにswagger-uiを新規作成した.内容は以下の通りである.
  • 
    
    
        
        NEW Swagger UI
        
        
        
        
        
        
        
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
        
        
        
    
    
    
    
    
  • した Uable to infer base url.This 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 swagures the.aveble.avable.veble.http://example.org/api/v2/api-docs then the base url ishttp://example.org/api/. Please.enter the location manaualy:
  • これはJson が されていないので、 の2にあります.