SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

Posted 济世魔王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)相关的知识,希望对你有一定的参考价值。

pom.xml增加依赖

 1     <dependency>
 2         <groupId>io.springfox</groupId>
 3         <artifactId>springfox-swagger2</artifactId>
 4         <version>2.7.0</version>
 5      </dependency>
 6      <dependency>
 7         <groupId>io.springfox</groupId>
 8         <artifactId>springfox-swagger-ui</artifactId>
 9         <version>2.7.0</version>
10      </dependency>

springboot 使用的1.5.7.RELEASE

 

package com.example.thymeleafdemo;

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.example.thymeleafdemo.controller"))
                .paths(PathSelectors.any())
                .build();
    }


    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("测试整合")
                .termsOfServiceUrl("http://127.0.0.1:8080/")
                .contact("zwb测试")
                .version("1.0.0")
                .build();
    }
}

controller 

package com.example.thymeleafdemo.controller;

import com.example.thymeleafdemo.domain.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/users")
public class UserController {

    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",paramType = "path",required = true, 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",paramType = "path", required = true, 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",paramType = "path", required = true, dataType = "Long")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "success";
    }
}

 

如果 @ApiImplicitParam(name = "id", value = "用户ID",paramType = "path", required = true, dataType = "Long") 没有paramType = "path"会提示类型转换String convert to Long错误。

 

以上是关于SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成Swagger2在线文档

SpringBoot_springboot集成swagger2

Spring Boot 入门:集成 swagger2

SpringBoot集成Swagger2生成API接口文档

SpringBoot集成Swagger2生成API接口文档

springboot 集成swagger2