当前位置:   article > 正文

Swagger API文档Responses中Object类型无法显示,求指引_openapi3 response对象不显示

openapi3 response对象不显示

问题: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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号