SpringBoot集成Swagger步骤详解及遇到类型转换错误+400
Posted danfengw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot集成Swagger步骤详解及遇到类型转换错误+400相关的知识,希望对你有一定的参考价值。
在[ SpringBoot入门(五)数据库操作入门]文章的基础上继续的(http://blog.csdn.net/danfengw/article/details/77435168)
1、Swagger在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>
2、在启动application的相同目录下创建Swagger2
@Configuration
@EnableSwagger2
public class Swagger2
@Bean
public Docket createRestApi()
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
private ApiInfo apiInfo()
return new ApiInfoBuilder()
.title("SpringBoot 中使用Swagger构建Restful APIs")
.description("更多SpringBoot 请查看官网")
.termsOfServiceUrl("https://swagger.io/")
.version("1.0")
.build();
通过@Configuration注解,让Spring来加载该类配置。再通过@EnableSwagger2注解来启用Swagger2。
再通过createRestApi函数创建Docket的Bean之后,apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,本例采用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore指定的请求)。
3、创建Controller文件
相关注解比如下面的@Api@ApiOperation参考地址:
https://github.com/swagger-api/swagger-core/wiki/Annotations
@RestController
@Api(value = "demo演示", description = "主要提供演示测试相关的接口API")
@RequestMapping("/person") //wentiz ehzai wen问题这这
public class PersonController
@Autowired
private PersonRepository personRepository;
private AtomicInteger atomicInteger = new AtomicInteger(0);
//增
@ApiOperation(value="创建用户" ,notes = "根据person对象创建用户")
@ApiImplicitParams(
@ApiImplicitParam(name = "name",value = "用户名",required =true,dataType = "String",paramType = "form"),
@ApiImplicitParam(name = "age",value = "用户年龄",required = true,dataType = "Integer",paramType = "form")
)
@RequestMapping(value = "",method = RequestMethod.POST)
public Person postUser(@RequestParam(value = "name") String name,@RequestParam(value = "age") Integer age)
Integer id=atomicInteger.incrementAndGet();
Person p=new Person();
p.setName(name);
p.setAge(age);
p.setId(id);
return personRepository.save(p);
//删
@ApiOperation(value = "删除用户",notes="根据id删除用户")
@ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer",paramType = "path")
@RequestMapping(value = "/id",method = RequestMethod.DELETE)
public String deletePerson(@PathVariable Integer id)
personRepository.delete(id);
return "success";
//改
@ApiOperation(value = "更新用户信息",notes = "根据id")
@ApiImplicitParams(
@ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer",paramType = "path"),
@ApiImplicitParam(name = "person" ,value="用户信息",required = true,dataType = "Person")
)
@RequestMapping(value = "/id",method=RequestMethod.PUT)
public String updatePerson(@PathVariable Integer id,@RequestBody Person person)
Person p=new Person();
person.setId(id);
person.setName(person.getName());
person.setAge(person.getAge());
personRepository.save(p);
return "success";
//查
@ApiOperation(value="获取用户列表",notes = "获取全部用户")
@RequestMapping(value = "",method = RequestMethod.GET)
public List<Person> getPersons()
return personRepository.findAll();
@ApiOperation(value = "获取用户信息",notes="根据id来获取用户信息")
@ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "Integer",paramType = "path")
@RequestMapping(value = "/id",method = RequestMethod.GET)
public Person getPerson(@PathVariable Integer id)
return personRepository.findOne(id);
浏览器地址:http://localhost:8082/swagger-ui.html
注意:这里的PersonController 必须有@RequestMapping(“/person”)注解否则会出现如下错误:
Failed to convert value of type ‘java.lang.String’ to required type ‘java.lang.Integer’; nested exception is java.lang.NumberFormatException: For input string: “swagger-ui”
(ps:这里好坑,网上很多例子也都没有这个路径,找了半天,最后还是找了大神帮忙给解决的)
4、其他的类
PersonRepository
public interface PersonRepository extends JpaRepository<Person,Integer>
Person这里没有给出setget方法,通过使用lombok插件生成,插件只需要在pom.xml文件中进行配置一下就可以了。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
Person.java
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Data//lombok
public class Person
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
@GeneratedValue相关解释:http://blog.csdn.net/u012493207/article/details/50846616
创作打卡挑战赛 赢取流量/现金/CSDN周边激励大奖以上是关于SpringBoot集成Swagger步骤详解及遇到类型转换错误+400的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot——SpringBoot集成Swagger生成API文档
Springboot系列 集成接口文档swagger,使用,测试