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

pom.xml增加依赖包

    
        io.springfox
        springfox-swagger2
        2.2.2
    
    
        io.springfox
        springfox-swagger-ui
        2.2.2
    

编写swapper2配置类

package com.springboot.config;

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
//该注解实现让spring知道这个是配置的类
@EnableSwagger2
//注解来启用Swagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.springboot.ctrl"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("description")
                .termsOfServiceUrl("termsOfServiceUrl")
                .contact("hp")
                .version("1.0")
                .build();
    }

}

Controller内使用

@ApiOperation(value = "更新用户详细信息", notes = "更新用户详细信息notes")
    @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";
    }

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

你可能感兴趣的:(SpringBoot)