swagger2: restful management project API tool
1. Add dependency packages to pom.xml
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> Be sure to use 2.6.1, do not use 2.4.0, it will report an error if it is 2.4.0, it will report this error Strange exception in logs "ClassNotFoundException: org.mapstruct.Mapper
2. Declare the swagger configuration bean in spring-mvc.xml
<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
3. Configure the resource file in spring -mvc.xml
mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/> <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
4. Configure the scan path in spring-mvc.xml
<mvc:annotation-driven /> <context:component-scan base-package="com.kind.perm.api" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
4. Configure the scan path in the Controller
package com.kind.perm.api.demo; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.kind.common.dto.DataGridResult; import com.kind.common.persistence.PageView; import com.kind.perm.api.common.controller.BaseController; import com.kind.perm.core.demo.domain.CommunityDO; import com.kind.perm.core.demo.service.CommunityService; import com.kind.perm.core.system.service.CoFileService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; /** * Because the functions are relatively simple, the service layer is not written * * @author hao.su */ @Api(description = "User Information") @RestController @RequestMapping("/user") public class UserController extends BaseController{ // service injection original routine // @Autowired // private CoFileService coFileService; @Autowired private CommunityService communityService; @ApiOperation(value = "Query User", notes = "Query User", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/user.json", params = "name", method = RequestMethod.GET) @ResponseBody public Object selectUser( @ApiParam(required = true, value = "用户名") @RequestParam(required = true) String name) { System.out.println("param name:" + name); Map<String, Object> map = new HashMap<String,Object>(); map.put("status", 0); map.put("data", ""); map.put("message", null); return map; } /** * Get paginated query list data. */ @ApiOperation(value = "Query List", notes = "Query List", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "selectPageList", method = RequestMethod.GET) @ResponseBody public DataGridResult selectPageList(CommunityDO query, HttpServletRequest request) { PageView<CommunityDO> page = communityService.selectPageList(query); return super.buildDataGrid(page); } }
The returned json format of DataGridResult I defined, the parent class defined by BaseController itself should not be entangled,
Basically, the above is enough. Note that when using jetty, you must configure the project name. If you do not configure the project name, there will be a problem, and a js error will be reported.
swagger2 will not manually introduce static resources like swagger1, which is very convenient
Notice:
If it fails, it mainly depends on whether spring-mvc.xml and web.xml are very useful for intercepting or not. The configuration is very idiot and it is very suitable for us to move bricks.