Swagger报错 TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
加上了@ApiImplicitParam注解后,swagger出现的报错信息
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
@ApiOperation("分页查询品牌列表")
@ApiImplicitParams({
@ApiImplicitParam(name="pageNum",value="当前页No.",required=true,paramType="form"),
@ApiImplicitParam(name="pageSize",value="每页显示记录数",required=true,paramType="form")
})
public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
List<PmsBrand> brandList = demoService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
@ApiOperation("获取指定id的品牌详情")
@ApiImplicitParam(name="id",value="记录Id",required=true,paramType="form")
public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
return CommonResult.success(demoService.getBrand(id));
}
TypeError: Failed to execute ‘fetch’ on ‘Window’: Request with GET/HEAD method cannot have body.
尝试把swagger的参数注解删除,错误消失。
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
@ApiOperation("分页查询品牌列表")
public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
List<PmsBrand> brandList = demoService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
@ApiOperation("获取指定id的品牌详情")
public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
return CommonResult.success(demoService.getBrand(id));
}
发现问题:swagger上显示的参数类型发生了变化
1.在加上注解的代码后显示的参数类型是formData
2.删除注解代码后显示的参数类型是path
所以去查了一下参数类型对应的内容:
paramType:表示参数放在哪个地方
header-->请求参数的获取:@RequestHeader(代码中接收注解)
query-->请求参数的获取:@RequestParam(代码中接收注解)
path(用于restful接口)-->请求参数的获取:@PathVariable(代码中接收注解)
body-->请求参数的获取:@RequestBody(代码中接收注解)
form(不常用)
修改注解代码的参数类型,问题解决。
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
@ApiOperation("分页查询品牌列表")
@ApiImplicitParams({
@ApiImplicitParam(name="pageNum",value="当前页No.",required=true,paramType="query"),
@ApiImplicitParam(name="pageSize",value="每页显示记录数",required=true,paramType="query")
})
public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
List<PmsBrand> brandList = demoService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
@ApiOperation("获取指定id的品牌详情")
@ApiImplicitParam(name="id",value="记录Id",required=true,paramType="path")
public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
return CommonResult.success(demoService.getBrand(id));
}