ApiResponses 混淆了 Swagger-UI 输出,找不到 "type"
ApiResponses is confusing Swagger-UI output to not find the "type"
我并不是真正的 java 开发人员(15 年前我是开发人员),但我正在努力更新我的知识,以便在这些项目中发挥更大的价值。我正在尝试将 swagger-ui 合并到一个基本的 rest springboot 应用程序中。我收到以下错误:
Unknown Type: CustomerVisit
我定义了以下控制器:
package controller;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RequestMapping("/api/v1")
@RestController
public class ScenarioController {
@Autowired
ScenarioService scenarioService;
@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
@ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(type="CustomerVisit"))),
@ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
@ApiResponse(responseCode="400", description="request not accepted, invalid input")
})
public ResponseEntity<?> getCustomerVisitByScenarioId(@PathVariable String scenarioIdStr) {
try {
int scenarioId = Integer.valueOf(scenarioIdStr);
List<CustomerVisit> customerList = scenarioService.getScenarioById(scenarioId);
if (customerList.size() > 0) {
return new ResponseEntity<>(customerList, HttpStatus.OK);
}
return new ResponseEntity<>(customerList, HttpStatus.ACCEPTED);
} catch (NumberFormatException e) {
return new ResponseEntity<>( "Scenario ID must be integer.", HttpStatus.BAD_REQUEST);
}
}
}
CustomerVisit bean 为
package bean;
@Schema(name="CustomerVisit", description="A model of a customer")
public class CustomerVisit {
public CustomerVisit(/* cut */) {
/* cut - constructing class */
}
/* cut getters */
}
swagger-ui 出现了,对于我没有尝试在文档中提供 ApiResponses
的其他方法,一切正常。一旦我添加了 ApiResponses,事情就开始崩溃了。如果我不添加 @Content
注释,ui 将响应类型描述为 {}
。如果我添加在 bean 中标记的类型的名称 - 它表示类型未知。我一直没能找到关于 gui 的文档,我希望能在正确的方向上得到推动。
看起来有旧版本的答案...
更新有效但丑陋的方法
- 添加了扩展 ArrayList
的静态 class
- 公开那个
这样
public static class CustomerVisitList extends ArrayList<CustomerVisit> {}
和
@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
@ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisitList.class))),
@ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
@ApiResponse(responseCode="400", description="request not accepted, invalid input")
})
有没有更好的方法?
这似乎是朝着正确方向迈出的一步(这是不对的)——但它确实应该return返回一个列表:
@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
@ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisit.class))),
@ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
@ApiResponse(responseCode="400", description="request not accepted, invalid input")
})
我并不是真正的 java 开发人员(15 年前我是开发人员),但我正在努力更新我的知识,以便在这些项目中发挥更大的价值。我正在尝试将 swagger-ui 合并到一个基本的 rest springboot 应用程序中。我收到以下错误:
Unknown Type: CustomerVisit
我定义了以下控制器:
package controller;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RequestMapping("/api/v1")
@RestController
public class ScenarioController {
@Autowired
ScenarioService scenarioService;
@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
@ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(type="CustomerVisit"))),
@ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
@ApiResponse(responseCode="400", description="request not accepted, invalid input")
})
public ResponseEntity<?> getCustomerVisitByScenarioId(@PathVariable String scenarioIdStr) {
try {
int scenarioId = Integer.valueOf(scenarioIdStr);
List<CustomerVisit> customerList = scenarioService.getScenarioById(scenarioId);
if (customerList.size() > 0) {
return new ResponseEntity<>(customerList, HttpStatus.OK);
}
return new ResponseEntity<>(customerList, HttpStatus.ACCEPTED);
} catch (NumberFormatException e) {
return new ResponseEntity<>( "Scenario ID must be integer.", HttpStatus.BAD_REQUEST);
}
}
}
CustomerVisit bean 为
package bean;
@Schema(name="CustomerVisit", description="A model of a customer")
public class CustomerVisit {
public CustomerVisit(/* cut */) {
/* cut - constructing class */
}
/* cut getters */
}
swagger-ui 出现了,对于我没有尝试在文档中提供 ApiResponses
的其他方法,一切正常。一旦我添加了 ApiResponses,事情就开始崩溃了。如果我不添加 @Content
注释,ui 将响应类型描述为 {}
。如果我添加在 bean 中标记的类型的名称 - 它表示类型未知。我一直没能找到关于 gui 的文档,我希望能在正确的方向上得到推动。
看起来有旧版本的答案...
更新有效但丑陋的方法
- 添加了扩展 ArrayList 的静态 class
- 公开那个
这样
public static class CustomerVisitList extends ArrayList<CustomerVisit> {}
和
@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
@ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisitList.class))),
@ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
@ApiResponse(responseCode="400", description="request not accepted, invalid input")
})
有没有更好的方法?
这似乎是朝着正确方向迈出的一步(这是不对的)——但它确实应该return返回一个列表:
@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
@ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisit.class))),
@ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
@ApiResponse(responseCode="400", description="request not accepted, invalid input")
})