1

我试图让 SwaggerUI 看起来适合我。我有一堆 POST 方法,SwaggerUI 确实已经在 Web-UI 中生成了响应和请求正文,但是请求正文不正确。如何为我的 POST 方法创建自定义请求正文?

SpringFoxConfig是_

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.collect.ImmutableList;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig {
    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                //.globalResponseMessage(RequestMethod.POST, ImmutableList.of(new ResponseMessage(200, "Some global OK message",null)))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()            
                .apiInfo(getApiInfo()); 
    }

    private ApiInfo getApiInfo() {
        return new ApiInfo("RESTlike API",
                "An Api to call functions",
                "",
                "",
                "",
                "",
                "");
    }
}

例如 UI 中显示的架构是

{
"da": "MD5",
  "data": {
    "value": [
      "string"
    ]
  }
}

这不是正确的输入,并且会引发错误。在这个例子中,正确的输入是:

{
"da": "MD5",
"data": "String"
}

架构的数据来自哪里以及如何更改/覆盖它。

相应的方法

@ResponseBody
@PostMapping("/digest")
public StringWrapper digestData(@RequestBody DigestDataContainer params) throws IOException {
    return new StringWrapper(//code);
}

DigestDataContainer 只包含一个 bytearray 'data' 和一个 enum 'da'

编辑

好的,我发现为什么生成的响应体是错误的,swagger通过查看公共参数和所有getter来确定请求体的参数。由于我的方法中不仅有与参数相关的 getter,因此 UI 中显示的内容太多。您可以通过使用注释来防止在 UI 中显示参数

@ApiModelProperty(required = false, hidden = true)

但是,我的每种方法都没有唯一的正文,我已经对其中一些进行了分组,这导致 UI 中生成的请求正文与其所在的方法不准确。因此我不想显示UI 中的 body。

  • 有没有办法禁用请求的模型模式?
4

1 回答 1

1

最简单的(就像我一样)方法是使用 DTO 对象。

在你的情况下,而不是你digestData(@RequestBody DigestDataContainer params)只是使用方法

@ResponseBody
@PostMapping("/digest")
public StringWrapper digestData(@RequestBody DigestDataContainerDTO ddcDTO) throws IOException {

    DigestDataContainer ddc = new DigestDataContainer(ddcDTO);

    // process your DigestDataContainer object as before
}

DigestDataContainerDTO在哪里

public class DigestDataContainerDTO {
    private String da;
    private String data;
    // getters/setters
}

DigestDataContainer(ddcDTO)从. DigestDataContainer_DigestDataContainerDTO

于 2019-03-11T15:36:43.403 回答