1.pom.xml增加依赖包

 <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

2.编写swapper2配置类

package com.zyank;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zyank.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Spring Boot中试用Swagger2构建的RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://blog.didispace.com/")
.contact("leo")
.version("1.0")
.build();
}
}

3.启动springboot,访问http://localhost:8081/swagger-ui.html
会发现页面显示报错:

后台报错:

2017-07-02 15:56:51.988 WARN 7176 --- [ qtp20577666-17] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/swagger-ui.html] in DispatcherServlet with name 'dispatcherServlet'

想一下swagger-ui.html 是在springfox-swagger-ui.jar里的

如何才能让我们能访问到swagger-ui.html???
【百度到,MARK  http://www.jianshu.com/p/840320d431a1】

Spring Boot自动配置本身不会自动把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面。我们加上这个映射即可。

/**
* <Description> <br>
*
* @author luoluocaihong<br>
* @version 1.0<br>
* @taskId <br>
* @CreateDate Jul 2, 2017 <br>
* @since V8.0<br>
* @see XXXX <br>
*/
@Configuration
public class WebMVCConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
                 .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

}

再次启动springboot,访问http://localhost:8081/swagger-ui.html,OK:

4. Controller内使用

package com.zyank.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.zyank.domain.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value="/users")
public class UserContrller {
static Map<Long, User> users=Collections.synchronizedMap(new HashMap<Long,User>());
@ApiOperation(value="获取用户列表",notes="")
@RequestMapping(value={""},method=RequestMethod.GET)
public List<User> getUserList(){
List<User> r=new ArrayList<User>(users.values());
return r;
}
@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@RequestMapping(value="", method=RequestMethod.POST)
public String postUser(@RequestBody User user) {
users.put(user.getId(), user);
return "success";
}
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType="path", dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
}
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType="path", dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody User user) {
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";
}
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
users.remove(id);
return "success";
}
}

如果上诉代码没有写paramType = “path” 会提示类型转换String convert to Long错误。

由此看来,springboot集成Swagger2对接口有入侵,使用需谨慎。

SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)的更多相关文章

  1. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  2. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  3. springboot集成swagger2报Illegal DefaultValue null for parameter type integer

    springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...

  4. SpringBoot使用Swagger2实现Restful API

    很多时候,我们需要创建一个接口项目用来数据调转,其中不包含任何业务逻辑,比如我们公司.这时我们就需要实现一个具有Restful API的接口项目. 本文介绍springboot使用swagger2实现 ...

  5. 集成swagger2构建Restful API

    集成swagger2构建Restful API 在pom.xml中进行版本管理 <swagger.version>2.8.0</swagger.version> 给taosir ...

  6. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  7. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  8. win7 iis7.5 配置错误解决办法

    win7 iis7.5 配置HTTP 错误 404.3 在初次使用IIS7的时候经常遇到的一个错误解决办法1: 找到Visual Studio命令提示工具,运行aspnet_regiis.exe -i ...

  9. Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法

    Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法 登陆数据库时提示 “ORA-01033”错误在命令窗口以s ...

随机推荐

  1. hdu-6434-欧拉函数

    Problem I. Count Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  2. Async:简洁优雅的异步之道

    前言 在异步处理方案中,目前最为简洁优雅的便是 async函数(以下简称A函数).经过必要的分块包装后,A函数能使多个相关的异步操作如同同步操作一样聚合起来,使其相互间的关系更为清晰.过程更为简洁.调 ...

  3. 字符序列(characts)

    字符序列(characts) 问题描述: 从三个元素的集合[A,B,C]中选取元素生成一个N 个字符组成的序列,使得没有两个相邻的 子序列(子序列长度=2)相同,例:N=5 时ABCBA 是合格的,而 ...

  4. EvalAI使用——类似kaggle的开源平台,不过没有kernel fork功能,比较蛋疼

    官方的代码 https://github.com/Cloud-CV/EvalAI 我一直没法成功import yaml配置举办比赛(create a challenge on EvalAI 使用htt ...

  5. MSOCache office问题

    \MSOCache 链接:https://pan.baidu.com/s/1dVvjYnD0D6DG6oFK_Mww2w密码:qrvp undefied

  6. js简单验证码的生成和验证

    如何用js生成简单验证码,并验证是否正确的方法 1.html页面如下 <div> <table border="0" cellspacing="5&qu ...

  7. BeanUtils.copyProperties(A,B)使用注意事项

    ***最近项目中用到BeanUtils.copyProperties(),然后踩了一些坑,也在网上查看了很多同行的测试和总结,现在将自己的测试.整理的注意事项分享如下,希望大家一起学习进步. ***注 ...

  8. centos6.5 安装PHP7.0支持nginx

    1.安装PHP所需要的扩展           yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel ...

  9. Java 实现倒计时(由秒计算天、小时、分钟、秒)

    public class Countdown4 { private static long day = 0; private static long hour = 0; private static ...

  10. C# 爬虫DLL文件 学习网站

    http://blog.csdn.net/u013063099/article/details/73201649?locationNum=15&fps=1 http://www.cnblogs ...