springmvc 加入swagger2

swagger2:restful管理项目API工具

1、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>  

一定要用2.6.1 不要用2.4.0 会报错误的 如果是2.4.0会报这个错误
Strange exception in logs "ClassNotFoundException: org.mapstruct.Mapper

 

2、在spring-mvc.xml中声明swagger配置bean

<bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
 

 

3、在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、在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、在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;

/**
 * 由于功能都比较简单 故没有写service层
 *
 * @author hao.su
 */
@Api(description = "用户信息")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
	
	
//	service的注入原来的套路
//	@Autowired
//	private CoFileService coFileService;
	@Autowired
	private CommunityService communityService;

	@ApiOperation(value = "查询用户", notes = "查询用户", 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;
	}
	
	/**
	 * 获取分页查询列表数据.
	 */
	@ApiOperation(value = "查询列表", notes = "查询列表", 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);
	}

}

DataGridResult  我自己定义的返回的json格式,BaseController自己定义的父类 不要纠结, 

 

 

基本以上就可以了,注意用jetty的时候一定要配置项目的名称,如果不配置项目名称是有问题的会报js错误

swagger2 也不会像 swagger1 一样还要手动引入静态的资源,这都不用的很方便

注意:

    如果失败主要看spring-mvc.xml 和 web.xml 有没有拦截什么的 非常好用,配置很白痴很适合咱们这种搬砖的用。

 

 

猜你喜欢

转载自394498036.iteye.com/blog/2391720