若鱼的专栏

JAVA开发

Swagger填坑

关于返回的类型response

(1)不支持泛型。解决办法:

@RequestMapping(value = "/image",method = RequestMethod.POST, produces = { "application/json" })
    @ApiOperation(value = "附件上传(图片),支持批量", notes = "", response = UploadImageResult.class)
    public Result<List<UploadResult>> imageUpload(final HttpServletRequest request,
                                                  @ApiParam(name="任意",value="图片上传") MultipartFile file1,
                                                  @ApiParam(name="任意",value="图片上传") MultipartFile file2,
                                                  @ApiParam(name="任意",value="图片上传") MultipartFile file3) throws Exception{
        return upload(request, AttachType.IMAGE);
    }
根据方法的实际返回类型,重新定义一个类型,在里面把泛型处理掉。

public class Result<T> {
    private String code;
    private String msg;
    private T data;
}
public class UploadImageResult {
    private String code;
    private String msg;
    private List<UploadResult> data;
}

(2)返回的bean中如果有boolean类型,请注意,swagger识别的bean跟标准的javabean的规范是不一样的。

标准的javabean:

private boolean isDelete;
public boolean isDelete() {
	return isDelete;
}
public void setDelete(boolean delete) {
	isDelete = delete;
}
但是,对于swagger只能这样:

private boolean isDelete;
public boolean getIsDelete() {
	return isDelete;
}

public void setIsDelete(boolean delete) {
	isDelete = delete;
}

关于文件上传:

swagger并不支持(MultipartFile[] files)这种参数,如果是多个文件,只能写多个MultipartFile


未完待续。。。

---------------------------------------泛型现在已经支持了!!!

---------------------------------------泛型现在已经支持了!!!

---------------------------------------泛型现在已经支持了!!!

---------------------------------------泛型现在已经支持了!!!

---------------------------------------泛型现在已经支持了!!!

---------------------------------------泛型现在已经支持了!!!

@RequestMapping(value="/simple/get", method=RequestMethod.GET)
	@ApiOperation(value="单个参数,GET")
	public Result<String> simpleGet(@ApiParam("这是p1参数") @RequestParam("p1")String p1) throws Exception{
		return Result.success("hello,world");
	}
	
	@RequestMapping(value="/simple/get2", method=RequestMethod.GET)
	@ApiOperation(value="单个参数,GET2")
	@ApiImplicitParam(name="p1",value="这是p1参数", paramType="query")
	public Result<String> simpleGet2() throws Exception{
		return Result.success("hello,world");
	}
效果是这样的:



@RequestMapping(value="/complex/get", method=RequestMethod.POST)
	@ApiOperation(value="对象参数")
	public Result<User> complexGet(@ApiParam @RequestBody User user) throws Exception{
		user.setId("newid");
		return Result.success(user);
	}

@ApiModel(value="用户对象")
public class User {
	@ApiModelProperty(value="用户ID")
	private String id;
	@ApiModelProperty(value="用户姓名")
	private String name;
	@ApiModelProperty(value="用户昵称")
	private String nickName;
	@ApiModelProperty(value="用户openid", hidden=true)
	private String openid;
	@ApiModelProperty(value="用户手机号", example="13112341234", required=true)
	private String mobile;
}

效果是这样的:


@RequestMapping(value="/complex/get2", method=RequestMethod.POST)
	@ApiOperation(value="对象参数")
	@ApiImplicitParams(
			{
				@ApiImplicitParam(name="id", value="用户ID", paramType="query"),
				@ApiImplicitParam(name="name", value="用户姓名", paramType="query")
			}
	)
	public Result<User> complexGet2(@ApiParam @RequestBody User user) throws Exception{
		user.setId("newid");
		return Result.success(user);
	}
效果是这样的:



上面这几种情况掌握就完全足够用了!


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/goldenfish1919/article/details/51791347
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Swagger填坑

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭