要求:我有一个POST方法,它接受输入JSON作为字符串,并将其传递给另一个微服务。我不想创建此输入JSON的对象(Bean)。
方法:
@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){
}
问题:生成的Swagger没有将输入显示为显示所有JSON属性的JSON模型。
期望:我想要显示我的Swagger,如下所示:
毫无疑问,我遗漏了关键的东西。有什么想法吗?
发布于 2017-12-31 17:34:17
如果不能从String
更改为具体的对象(尽管这是我建议您做的,因为它更简洁),您可以尝试使用@ApiImplicitParams
(查看他们的documentation)
@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@ApiImplicitParams({
@ApiImplicitParam(name = "Object", value = "Object to be created", required = true, dataType = "your.package.BodyClass", paramType = "body")
})
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){
}
(不确定是否还需要方法参数中的@Apiparam(name="JSONrequest", required = true)
位)
发布于 2020-09-29 22:46:08
这是一个老问题,但由于我在这里还没有找到在线解决方案,我如何在由java注解自动生成的swagger文档中自定义示例值。我使用swagger 2.0和springfox.version 2.10.5。
其思想是记录具有@RequestBody
注释的请求参数的类。例如,我的方法是
@ApiOperation(
value = "Start ListBuilder extraction",
response = ExtractionLogEntity.class,
produces = "application/json"
)
@PostMapping("/extraction/start")
public ExtractionLogEntity startTask(
@RequestBody(required = true) ExtractionRequest request,
为了公开request json对象示例,我在ExtractionRequest的属性中添加了一个@ApiModelProperty(example = "...")
注释。
@ApiModelProperty(example = "[{ 'field':'value'}]")
@NotNull
private List<ListBuilderFieldEntity> fields;
@ApiModelProperty(example = "1000")
private String ied;
@ApiModelProperty(example = "US")
private String codebase;
这就是结果
发布于 2020-09-26 12:36:44
我也遇到过类似的问题。我的服务类在字符串中接受@RequestBody参数。所以,我所做的是:
创建了一个POJO,并使用@RequestBody注解代替了inputString。
@RequestMapping(value = "/api/entity/{entityId}/user/query", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
ResponseEntity<String> queryUser(@PathVariable("entityId") String entityId,
@RequestBody QueryUserJsonSchemaPOJO queryUserJsonSchemaPOJO, String inputString,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return userService.queryUserService(inputString, entityId, request);
}
创建了一个带有@Around注解的AOP,该注解更新了inputString参数。
@Around(value = "execution(* com.athmin.rest.UserController.*(..)) || execution(* com.athmin.rest.CityController.*(..)), and args(..) " +
" && @annotation(com.athmin.annotations.JSONSchemaFileName) ")
public Object validateRequestBodyAgainstJsonSchema(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object[] modifiedArgs = proceedingJoinPoint.getArgs();
for (Object o : proceedingJoinPoint.getArgs()) {
if (o instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) o;
requestBody = httpServletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
}
});
for (int i = 0; i < modifiedArgs.length; i++) {
if (modifiedArgs[i] == null) { // Only inputString is null in my case
modifiedArgs[i] = requestBody;
}
}
proceedingJoinPoint.proceed(modifiedArgs);
}
https://stackoverflow.com/questions/48039876
复制相似问题