`

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 有没有拦截什么的 非常好用,配置很白痴很适合咱们这种搬砖的用。

 

 

分享到:
评论

相关推荐

    SpringMVC 集成 Swagger2

    本文将详细介绍如何在SpringMVC项目中集成Swagger2,以实现API的自动化文档生成。 首先,我们需要了解Swagger2的核心组件。Swagger2的核心是`@Api`和`@ApiOperation`注解,它们用于标记API接口和方法,以便Swagger...

    SpringMVC+SwaggerUI

    SpringMVC和SwaggerUI是两个在Java Web开发中非常重要的工具。SpringMVC是Spring框架的一部分,用于构建高效、灵活的Web应用程序,而SwaggerUI则是一个用于生成、测试和展示RESTful API的交互式界面。这个名为...

    springmvc与swagger整合使用的包 普通web项目的jar工程包

    在IT行业中,SpringMVC和Swagger的整合是一个常见的需求,特别是在构建RESTful API服务时,为了提供清晰的接口文档和方便的API测试。SpringMVC是Spring框架的一部分,用于处理HTTP请求,而Swagger则是一个强大的工具...

    java springMVC+swagger dome

    2. **交互式测试**:Swagger UI允许开发者在浏览器中直接测试API,无需编写任何测试代码。只需点击界面上的操作,输入参数,即可看到实时的HTTP请求和响应。 3. **API版本管理**:Swagger支持API版本控制,使得在不...

    SpringMVC如何在生产环境禁用Swagger的方法

    SpringMVC如何在生产环境禁用Swagger的方法 Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,...

    springmvc与swagger swagger-ui 集成demo

    标题 "springmvc与swagger swagger-ui 集成demo" 涉及到的是在Spring MVC框架下集成Swagger和Swagger-UI的技术实践。Spring MVC是Java Web开发中的一个流行框架,用于构建可重用、模块化的Web应用程序。Swagger则是...

    springmvc集成swagger-ui

    Springfox是提供Swagger支持的库,包含两个主要组件:`springfox-swagger2` 和 `springfox-swagger-ui`。在你的`pom.xml` 或 `build.gradle` 文件中添加如下依赖: 对于Maven: ```xml &lt;groupId&gt;io.springfox ...

    SpringMVC集成Swagger

    在SpringMVC的配置类中,我们创建一个`@Configuration`注解的类,并且添加`@EnableSwagger2`注解来启用Swagger2。然后,我们需要创建一个`Docket`实例,该实例定义了Swagger的配置,例如API的基本信息、分组、及扫描...

    SpringMvc整合swagger2 .docx下载

    ### Spring MVC 整合 Swagger2 实现在线 API 文档测试 在现代软件开发过程中,API 文档对于提高团队协作效率、降低系统维护成本至关重要。Swagger 是一个非常流行的框架,能够帮助开发者快速创建、记录和使用 ...

    SpringMvc-SwaggerDemo

    标题“SpringMvc-SwaggerDemo”表明这是一个关于Spring MVC与Swagger集成的示例项目。Spring MVC是Spring框架的一部分,主要用于构建Web应用,提供模型-视图-控制器(MVC)架构。Swagger则是一个用于设计、构建、...

    springMVC Spring mybatis swagger demo

    2. **SpringMVC**:SpringMVC是Spring框架的一部分,专门处理Web请求。它通过DispatcherServlet接收HTTP请求,然后根据请求映射找到相应的Controller处理,Controller可以调用Service层方法,再由Service层调用DAO层...

    SpringMVC中Swagger对应的JAR包

    通常,这会涉及到Swagger的核心库`swagger-core`和UI库`swagger-ui`,以及SpringMVC的适配器`springfox-swagger2`和`springfox-swagger-ui`。在Maven项目中,可以在pom.xml文件中添加如下依赖: ```xml ...

    springmvc配置swagger2前端页面代码

    Swagger2 是在Spring MVC框架上实现的一个优秀的API文档工具,它能够自动生成RESTful API接口的文档,使得开发者无需编写繁琐的文档,就能轻松地提供清晰、完整的API接口说明。 Swagger2的核心组件包括`@Api`, `@...

    java-springmvc-swagger

    本示例"java-springmvc-swagger"提供了一个基于Spring MVC框架集成Swagger的简单Demo,旨在帮助开发者快速理解和掌握Swagger的基本配置和使用。 首先,让我们了解一下Swagger的核心功能。Swagger是一个用于设计、...

    SpringMVC和Swagger整合方法

    SpringMVC和Swagger整合方法 SpringMVC是一种基于Java的Web应用程序框架,用于构建Web应用程序,而Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。本文将为大家介绍SpringMVC...

    springmvc-swagger.zip

    Spring MVC 和 Swagger 是两个...这个压缩包"springmvc-swagger"很可能包含了一个示例项目,用户可以参考该项目的源代码,学习如何将Swagger2集成到Spring MVC项目中,以便在自己的开发实践中实现优雅的API管理和测试。

    springmvc整合swagger所需jar包

    4. `swagger-springmvc`或`springfox-swagger2`(如果是较新版本):提供了与Spring MVC的集成,能够自动扫描并解析Spring MVC的注解来生成Swagger的API定义。 整合步骤通常包括: 1. 添加依赖:在项目的`pom.xml`...

    SpringMVC集成Swagger实例代码

    SpringMVC集成Swagger实例代码 SpringMVC是一种流行的Java Web框架,Swagger是一种流行的API文档生成工具,两者的集成可以方便地生成API文档。本文将介绍如何将SpringMVC与Swagger集成,并提供实例代码。 标题解释...

    springmvc整合swagger示例代码

    &lt;artifactId&gt;springfox-swagger2 &lt;version&gt;2.9.2 &lt;groupId&gt;io.springfox &lt;artifactId&gt;springfox-swagger-ui &lt;version&gt;2.9.2 ``` 接下来,我们需要配置Swagger。创建一个名为`SwaggerConfig`的类,并使用`@...

    springmvc整合swagger所需文件

    2. **Spring MVC**:Spring MVC是Spring框架的一部分,专门用于构建Web应用。它提供了一种模型-视图-控制器(MVC)架构模式,使得开发者可以将业务逻辑、数据和用户界面分离,提高了代码的可维护性和可扩展性。 3. ...

Global site tag (gtag.js) - Google Analytics