意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

SpringBoot集成Swagger步骤详解及遇到类型转换错误+400

来源:恒创科技 编辑:恒创科技编辑部
2024-02-02 21:48:59


在[ SpringBoot入门(五)数据库操作入门]文章的基础上继续的

1、Swagger在pom.xml文件的配置


SpringBoot集成Swagger步骤详解及遇到类型转换错误+400

<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

SpringBoot集成Swagger步骤详解及遇到类型转换错误+400_java

@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​​

SpringBoot集成Swagger步骤详解及遇到类型转换错误+400_java_02

@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

浏览器地址:​​http://localhost:8082/swagger-ui.html​​

SpringBoot集成Swagger步骤详解及遇到类型转换错误+400_获取用户信息_03


SpringBoot集成Swagger步骤详解及遇到类型转换错误+400_spring_04


注意:这里的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:这里好坑,网上很多例子也都没有这个路径,找了半天,最后还是找了大神帮忙给解决的)

SpringBoot集成Swagger步骤详解及遇到类型转换错误+400_spring_05

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



上一篇: docker 运行javaweb,oracle数据库无法链接 下一篇: 手机怎么远程登录云服务器?