此处添加了ApiResponse注解,但在swagger中的响应依然没有数据
AjaxResult是继承的map,Swagger只认识定义好的类-属性,所以接口返回Map,对于Swagger来说是没有字段展示的。
如需要可以使用泛型类R
来解决这个问题。
package com.ruoyi.common.core.domain;
import java.io.Serializable;
/**
* 响应信息主体
*
* @author Roc-xb
*/
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功
*/
public static final int SUCCESS = 0;
/**
* 失败
*/
public static final int FAIL = 500;
private int code;
private String msg;
private T data;
public static <T> R<T> ok() {
return R(null, SUCCESS, null);
}
public static <T> R<T> ok(T data) {
return R(data, SUCCESS, null);
}
public static <T> R<T> ok(T data, String msg) {
return R(data, SUCCESS, msg);
}
public static <T> R<T> fail() {
return R(null, FAIL, null);
}
public static <T> R<T> fail(String msg) {
return R(null, FAIL, msg);
}
public static <T> R<T> fail(T data) {
return R(data, FAIL, null);
}
public static <T> R<T> fail(T data, String msg) {
return R(data, FAIL, msg);
}
public static <T> R<T> fail(int code, String msg) {
return R(null, code, msg);
}
private static <T> R<T> R(T data, int code, String msg) {
R<T> r = new R<>();
r.setCode(code);
r.setData(data);
r.setMsg(msg);
return r;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
package com.ruoyi.web.controller.tool;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* swagger 用户测试方法
*
* @author Roc-xb
*/
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController {
private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
{
users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
}
@ApiOperation("获取用户详细")
@ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
@GetMapping("/{userId}")
@ApiResponses({
@ApiResponse(code = 0, message = "操作成功", response = UserEntity.class)
})
public R<UserEntity> getUser(@PathVariable Integer userId) {
return R.ok(users.get(userId));
}
}
@ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity {
@ApiModelProperty("用户ID")
private Integer userId;
@ApiModelProperty("用户名称")
private String username;
@ApiModelProperty("用户密码")
private String password;
@ApiModelProperty("用户手机")
private String mobile;
public UserEntity() {
}
public UserEntity(Integer userId, String username, String password, String mobile) {
this.userId = userId;
this.username = username;
this.password = password;
this.mobile = mobile;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
可以看到,响应信息里面已经显示出类的属性描述。
powered by kaifamiao