使用swagger 2.9.2版本,注解@ApiParam出现的问题

在前后端分离的项目中,为了便捷开发,很多项目中会用到swagger这个工具,其中使用@ApiParam在项目启动后访问swagger页面的时候出现了这样一个问题。

接口层代码:

@Api(description = "运动等级体系接口")
@Slf4j
@RestController
@RequestMapping("/SportSystem")
public class SportSystemController {

    @Autowired
    private SportSystemService sportSystemService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有sportSystem",notes="获取所有sportSystem,无需参数", httpMethod = "GET")
    public RetVO findAll(){
        List<SportSystem> systemList = sportSystemService.findAll();
        return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg(),systemList);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value="获取单个sportSystem",notes="获取单个sportSystem,需参数", httpMethod = "GET")
    public RetVO get(@ApiParam(name = "id",value = "id主键",required = true) @PathVariable ("id") Integer id) {
        SportSystem system = sportSystemService.findById(id);
        return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg(),system);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value="插入sportSystem",notes="插入sportSystem,需参数", httpMethod = "POST")
    public RetVO insert(@ApiParam(name = "sportSystem",value = "运动等级体系实体",required = true) @RequestBody SportSystem sportSystem) {
         sportSystemService.insert(sportSystem);
        return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg());
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value="修改sportSystem",notes="修改sportSystem,需参数", httpMethod = "PUT")
    public RetVO update(@ApiParam(name = "sportSystem",value = "运动等级体系实体",required = true) @RequestBody SportSystem sportSystem) {
            sportSystemService.update(sportSystem);
            return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg());
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value="删除单个sportSystem",notes="删除单个sportSystem,需参数", httpMethod = "DELETE")
    public RetVO delete(@ApiParam(name = "id",value = "id主键",required = true) @PathVariable ("id") Integer id) {
         sportSystemService.deleteById(id);
         return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg());
    }

}

在访问swagger网址时:http://localhost:8001/swagger-ui.html#/
出现了一个异常:

java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:601)
	at java.lang.Long.valueOf(Long.java:803)
	at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:633)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:616)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:519)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:31)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
	at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2980)
	at springfox.documentation.spring.web.json.JsonSerializer.toJson(JsonSerializer.java:38)
	at springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(Swagger2Controller.java:105)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)

经过分析是swagger中类型转换的问题,在@ApiParam注解中需要@PathVariable (“id”) Integer id映射的代码中加入属性
example = "1"对应上即可。

@Api(description = "运动等级体系接口")
@Slf4j
@RestController
@RequestMapping("/SportSystem")
public class SportSystemController {

    @Autowired
    private SportSystemService sportSystemService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有sportSystem",notes="获取所有sportSystem,无需参数", httpMethod = "GET")
    public RetVO findAll(){
        List<SportSystem> systemList = sportSystemService.findAll();
        return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg(),systemList);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value="获取单个sportSystem",notes="获取单个sportSystem,需参数", httpMethod = "GET")
    public RetVO get(@ApiParam(name = "id",value = "id主键",required = true,example = "1") @PathVariable ("id") Integer id) {
        SportSystem system = sportSystemService.findById(id);
        return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg(),system);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value="插入sportSystem",notes="插入sportSystem,需参数", httpMethod = "POST")
    public RetVO insert(@ApiParam(name = "sportSystem",value = "运动等级体系实体",required = true) @RequestBody SportSystem sportSystem) {
         sportSystemService.insert(sportSystem);
        return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg());
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value="修改sportSystem",notes="修改sportSystem,需参数", httpMethod = "PUT")
    public RetVO update(@ApiParam(name = "sportSystem",value = "运动等级体系实体",required = true) @RequestBody SportSystem sportSystem) {
            sportSystemService.update(sportSystem);
            return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg());
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value="删除单个sportSystem",notes="删除单个sportSystem,需参数", httpMethod = "DELETE")
    public RetVO delete(@ApiParam(name = "id",value = "id主键",required = true,example = "1") @PathVariable ("id") Integer id) {
         sportSystemService.deleteById(id);
         return new RetVO(RetCode.SUCCESS.getCode(),RetCode.MSG.getMsg());
    }

}

再次启动没有异常抛出!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值