Step through the Maven + SpringMVC + SpringFox + Swagger integration example

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wxm994116/article/details/79130845

More comprehensive examples see online, please click on the blue link

Step through the Maven + SpringMVC + SpringFox + Swagger integration example - the point I entered

Pit encountered during the process:

1. The configuration is complete, but can not enter the interface, because springMVC configured to intercept a result of landing rights, could not see the code which intercepts the request which, so only need to open the Google browser, enter the desired swagger-ui. html, then press F12 to see which url 404 errors, simply click go to view, add their own authority interception (for example, I landed on whether to intercept token), then put this url in xml or join an optional login interception of springmvc Code excluded!

2.springfox swagger2 related summary

a. How to make each url request to be added to add a fixed parameter? (Added directly to the required parameters in the configuration swaggerCofig request header, the following parameters token in the drawing) the reference link ( click to open the link ) the link 2 ( click to open the link )

    @Bean
    public Docket buildDocket(){
    	  ParameterBuilder tokenPar = new ParameterBuilder();  
          List<springfox.documentation.service.Parameter> pars = new ArrayList<springfox.documentation.service.Parameter>();  
          tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("query").required(false).build();  
          pars.add(tokenPar.build());  
          return new Docket(DocumentationType.SWAGGER_2)  
              .select()  
              .apis(RequestHandlerSelectors.any())  
              .paths(PathSelectors.any())  
              .build()  
              .globalOperationParameters(pars)  
              .apiInfo(apiInfo());  
    }
b. common parameter annotation, please click on the blue link on the right to view. I opened the link point

The following items commonly used in several notes will be briefly explained

@Api () for the class; swagger represent this class is identified resource  tags- explanatory  value- are described, alternate tags may be used  , however, if there are multiple tags value, generates a plurality of list 



@Api(value="用户controller",tags={"用户操作接口"})
@RestController
public class UserController {

}
Some can not use can lead to not display Chinese, so small series I will use this description of

@RestController
@Scope("prototype")
@Api(description = "用户信息管理")
public class UserController {
For some parameters carried in the request parameters, but not written in the controller method can be used to write notes swagger2 parameters, this also facilitates a number of interfaces without writing a parameter, you can directly write directly in this api interface! As shown below: (userCode and no password, it may be displayed in the interface controller in api, but may be qing)
@RequestMapping(value = "/user/login", method = { RequestMethod.GET })
	@ApiOperation(value = "用户登录", notes = "用户登录操作", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "用户编码或者用户邮箱或者用户电话或者用户的erp编码", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "token", value = "访问令牌(无需填写)", required = false, dataType = "String", paramType = "query") })
	public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {

The above is a plurality of parameters, the parameters may be single

@ApiImplicitParam(name = "phoneNum", value = "手机号码", required = false, dataType = "String", paramType = "path")
For the parameter descriptions @ApiImplicitParam

name- parameter names
value- Parameter Description 
dataType- data type (String / int / json / Formdata ( document) / Long)
paramType- parameter type (path (a path in the url) / query / body, etc.)
example- illustrated

c If the program is found can be accessed locally, but can not access other computers or to remote servers can not access the problem, should be cross-domain problem, you can choose to configure in web.xml

<filter>
		<filter-name>SimpleCORSFilter</filter-name>
		<filter-class>com.huiju.srm.app.filter.sys.SimpleCORSFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>SimpleCORSFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
Corresponding class codes as follows

package com.huiju.srm.app.filter.sys;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class SimpleCORSFilter implements Filter {
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE");
    response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type,x-csrf-token");
    response.setHeader("Content-type", "text/html;charset=UTF-8");
    System.out.println(response.getStatus());
    chain.doFilter(req, res);
  }
  public void init(FilterConfig filterConfig) {}
  public void destroy() {}
}















Guess you like

Origin blog.csdn.net/wxm994116/article/details/79130845