赞
踩
问题:Object类型和enum枚举值在API文档中展示异常
Result类
package com.dreamfuture.elearning.util; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Serializable; /** * 持续优化参考https://blog.csdn.net/WantFlyDaCheng/article/details/113874699 */ @ApiModel public class Result implements Serializable { static Logger logger = LoggerFactory.getLogger(Result.class); @ApiModelProperty(position = 0, value = "返回结果码") Object resultCode; @ApiModelProperty(position = 1, value = "返回数据体") Object data; /** * RestAPI成功应答报文 * * @param data * @return {@code Result} */ public static Result success(Object data) { Result result = Result.success(); result.setData(data); return result; } /** * RestAPI成功应答报文 * * @return {@code Result} */ public static Result success() { Result result = new Result(); result.setResultCode(ResultCode.SUCCESS); logger.info(System.currentTimeMillis() + " 成功"); return result; } /** * RestAPI拦截应答 * * @param response * @param resultCode * @return {@code HttpServletResponse} */ public static HttpServletResponse handlerResponse(HttpServletResponse response, ResultCode resultCode) throws IOException { Result result = new Result(); result.setResultCode(resultCode); logger.info(System.currentTimeMillis() + " " + resultCode.code() + resultCode.message()); return response; } /** * RestAPI拦截应答 * * @param response * @param resultCode * @param data * @return {@code HttpServletResponse} */ public HttpServletResponse handlerResponse(HttpServletResponse response, Object resultCode, Object data) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); Result result = new Result(); result.setResultCode(resultCode); result.setData(data); response.getWriter().append(result.toString()); return response; } /** * RestAPI失败应答报文 * * @param resultCode * @param data * @return {@code Result} */ public Result failure(Object resultCode, Object data) { Result result = new Result(); result.setResultCode(resultCode); result.setData(data); return result; } /** * RestAPI失败应答报文 * * @param resultCode * @return {@code Result} */ public static Result failure(Object resultCode) { Result result = new Result(); result.setResultCode(resultCode); return result; } public Object getResultCode() { return resultCode; } public void setResultCode(Object resultCode) { this.resultCode = resultCode; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
ResultCOde类
package com.dreamfuture.elearning.util; import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONType; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; /*@JSONType(serializeEnumAsJavaBean = true)*/ @ApiModel(value = "返回结果码") @JsonFormat(shape = JsonFormat.Shape.OBJECT)/*支持枚举值类型JSON格式返回*/ public enum ResultCode { /*成功状态码*/ SUCCESS(0, "成功"), /*1000~1999 区间表示参数错误*/ USER_EXIST(1001, "用户名已使用,请输入未使用用户名"), /*2000~2999 区间表示用户错误*/ USER_ERROR(2001, "用户账户或密码错误,登录失败"), USER_NOT_AUTH(2002, "尚未授权,请登录"), USER_AUTH_TIMEOUT(2003, "登录已失效,请重新登录"), /*3000~3999 区间表示接口异常*/ FAIL(9999, "未知错误"); //@JSONField(name = "code") @ApiModelProperty(value = "返回码",required = true) private Integer code; @ApiModelProperty(value = "返回码描述",required = true) private String message; ResultCode(Integer code, String message) { this.code = code; this.message = message; } public Integer code() { return this.code; } public String message() { return this.message; } public Integer getCode() { return code; } public String getMessage() { return message; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。