首页 > 解决方案 > Spring boot 上的 Swagger 2 - 我可以为 API 下显示的所需参数设置句号吗?

问题描述

我有这个 api 叫做/predict.

所需的参数有时可以是score1score2

info.score1info.score2取决于我们得到的数据集。

现在它在我的 localhost:8080/swagger-ui.html 上的样子是 在此处输入图像描述

以下是我如何实现它:

@ApiOperation(value="", response=RequestInput.class)
@RequestMapping(value="/predict", method= RequestMethod.POST, produces="application/json", consumes="application/json")
    public ResponseEntity predict(Map<String, Object> inputs, @RequestBody RequestInput requestInput) {
    ...
}

RequestInput.class看起来像这样

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

public class RequestInput {
    @ApiModelProperty(notes = "", required = true)
    @Getter private Double score1;
    @ApiModelProperty(notes = "", required = true)
    @Getter private Double score2;
}

我有两个问题:

  1. 有没有可能
{
  "info.score1":0,
  "info.score2":0
}
  1. 如何设置Responses零件的示例值?现在它反映了正在发生的事情requestInput。我希望它是
{
  "finalScore":0
}

标签: javaspring-bootswaggerswagger-uiswagger-2.0

解决方案


要处理请求中的动态属性值,您可以添加设置器setInfoScore1&setInfoScore2并用@JsonProperty

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

public class RequestInput {
    @ApiModelProperty(notes = "", required = true)
    @Getter private Double score1;
    @ApiModelProperty(notes = "", required = true)
    @Getter private Double score2;

    @JsonProperty("info.score1")
    public void setInfoScore1(Double score) {
        this.score = score;
    }

    @JsonProperty("info.score2")
    public void setInfoScore2(Double score) {
        this.score = score;
    }
}

推荐阅读