1. Overview of Swagger
Swagger is a canonical and complete framework for generating, describing, invoking, and visualizing RESTful Web services. The overall goal is to have clients and file systems update at the same rate as servers. File methods, parameters, and models are tightly integrated into server-side code, allowing the API to always be in sync. Swagger implements API and document consistency.
2. Add maven dependencies
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > <swagger.version>2.2.2</swagger.version> </properties> <dependencies> <dependency> <groupId> IO. Springfox </groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>Copy the code
3. Swagger configuration
/** * Swagger Configuration * @author * @date 2017/10/24. */ @configuration @enablesWagger2 public Class SwaggerConfig {@bean public DocketdemoApi() {return new Docket(DocumentationType.SWAGGER_2)
.groupName("demo") / / group name. GenericModelSubstitutes (DeferredResult. Class) / / generic template replacement useDefaultResponseMessages (false// Whether to use default response information. ForCodeGeneration (true)// Universal encoding. PathMapping ("/".select().paths(or(regex())"/api/.*"Build ().apiinfo (apiInfo()); } @Bean public DocketcreateRestApi() { Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() { @Override public boolean apply(RequestHandler requestHandler) { Class<? > declaringClass = requestHandler.getClass();if(declaringClass = = BasicErrorController. Class) / / outreturn false;
if(declaringClass isAnnotationPresent (RestController. Class)) / / be annotated classesreturn true;
if(declaringClass isAssignableFrom (ResponseBody. Class)) / / be annotated methodreturn true;
return false; }};return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.select()
.apis(predicate)
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact();
contact.setName("Chen Ping");
contact.setUrl("http://blog.csdn.net/dushiwodecuo");
contact.setEmail("[email protected]");
return new ApiInfoBuilder()
.title("Electronic Health Record(EHR) Platform API")//"EHR Platform's REST API, all the applications could access the Object model data via JSON."Version ()"1.0") / / version. TermsOfServiceUrl ("NO terms of service")
.contact("Chen Ping") / / author. License (The Apache License, Version 2.0)
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") .build(); }}Copy the code
4, test,
4.1. To configure the Controller
/ * *
Copy the code
- Controller
- @ the author Chen Ziping
@date 2017/10/24.
/
@Controller
@RequestMapping(value = "/api")
public class DemoController {
private Logger logger = LoggerFactory.getLogger(DemoController.class);
/ *- You can respond to JSON directly using @responseBody
@return
* /
@RequestMapping(value = "/getcount", method = RequestMethod.POST)
@apiOperation (value=" test-getCount ", notes="getCount ")
@ResponseBody
public ModelMap getCount() {
logger.info(">>>>>>>> begin getCount >>>>>>>>");
ModelMap map = new ModelMap();
map.addAttribute("count", 158);
// Internationalization information obtained in the background
Map.addattribute ("xstest", "test");
return map;
}
}
4.2 test
http://localhost:8080/swagger-ui.html