Java 如何使用SpringBoot和SpringFox在Swagger中将声明的主体类型从String更改为custom DTO类型
我有一个带有一种方法的rest控制器。此方法接受一个注释为Java 如何使用SpringBoot和SpringFox在Swagger中将声明的主体类型从String更改为custom DTO类型,java,swagger,swagger-ui,spring-restcontroller,springfox,Java,Swagger,Swagger Ui,Spring Restcontroller,Springfox,我有一个带有一种方法的rest控制器。此方法接受一个注释为@RequestBody的字符串参数。由于此处未提及的某些原因,我被迫使用typeString并手动将其转换为TestDTO。从API的使用者的角度来看,主体的类型是TestDTO,我想在SwaggerUI中显示这种类型 不幸的是(这很明显)swagger显示body是字符串类型的。看下面的图片 我想要实现的是在java代码中使用String主体,在swagger代码中使用TestDTO。我怎样才能强迫昂首阔步地表现出来呢?我试图找到注
@RequestBody
的字符串
参数。由于此处未提及的某些原因,我被迫使用typeString
并手动将其转换为TestDTO
。从API的使用者的角度来看,主体的类型是TestDTO
,我想在SwaggerUI中显示这种类型
不幸的是(这很明显)swagger显示body是字符串类型的。看下面的图片
我想要实现的是在java代码中使用String
主体,在swagger代码中使用TestDTO
。我怎样才能强迫昂首阔步地表现出来呢?我试图找到注释及其属性,但失败了
Rest控制器代码如下:
@RestController
@Api(tags = { "test" }, description = "test related resources")
public class TestController {
@Autowired
ObjectMapper mapper;
@RequestMapping(path = "/test", method = RequestMethod.POST)
public void confirm(@RequestBody String requestBody) throws IOException {
//do sth with body
TestDTO dto = mapper.readValue(requestBody, TestDTO.class);
//do sth with dto
}
}
class TestDTO{
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
请尝试在方法上添加此批注:
@ApiImplicitParam(name = "test", value = "testDTO", required = true, dataType = "TestDTO")
请尝试在方法上添加此批注:
@ApiImplicitParam(name = "test", value = "testDTO", required = true, dataType = "TestDTO")
我想出来了。需要作出两项改变
首先,如@Dave Pateral的回答中所述,必须添加@apimplicitparams
@RestController
@Api(tags = { "test" }, description = "test related resources")
public class TestController {
@Autowired
ObjectMapper mapper;
@ApiImplicitParams({
@ApiImplicitParam(name = "requestBody", required = true,
dataType = "TestDTO", paramType = "body")
})
@RequestMapping(path = "/test", method = RequestMethod.POST)
public void confirm(@RequestBody String requestBody) throws IOException {
//do sth with body
TestDTO dto = mapper.readValue(requestBody, TestDTO.class);
//do sth with dto
}
}
然后隐式模型必须在摘要中注册,这是下面最简单的工作示例
@Configuration
public class SwaggerConfiguration {
@Autowired
private TypeResolver typeResolver;
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.additionalModels(typeResolver.resolve(TestDTO.class));
}
}
结果是
我想出来了。需要作出两项改变 首先,如@Dave Pateral的回答中所述,必须添加@apimplicitparams
@RestController
@Api(tags = { "test" }, description = "test related resources")
public class TestController {
@Autowired
ObjectMapper mapper;
@ApiImplicitParams({
@ApiImplicitParam(name = "requestBody", required = true,
dataType = "TestDTO", paramType = "body")
})
@RequestMapping(path = "/test", method = RequestMethod.POST)
public void confirm(@RequestBody String requestBody) throws IOException {
//do sth with body
TestDTO dto = mapper.readValue(requestBody, TestDTO.class);
//do sth with dto
}
}
然后隐式模型必须在摘要中注册,这是下面最简单的工作示例
@Configuration
public class SwaggerConfiguration {
@Autowired
private TypeResolver typeResolver;
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.additionalModels(typeResolver.resolve(TestDTO.class));
}
}
结果是
这没用。另一个参数出现在大摇大摆的用户界面中。它没有帮助。另一个参数出现在大摇大摆的用户界面中。