我在Java SpringBoot中有一个API,我想在Swagger中记录它。
我已完成以下操作(我只包含包含与Swagger相关的代码的类):
主要课程
@EnableSwagger2
public class ProvisioningApiApplication {
public static void main(String[] args) {
if (AuthConfigFactory.getFactory() == null) {
AuthConfigFactory.setFactory(new AuthConfigFactoryImpl());
}
SpringApplication.run(ProvisioningApiApplication.class, args);
}
@Bean
public Docket swaggerSpringMvcPluggin() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
@Component
@Primary
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
setSerializationInclusion(JsonInclude.Include.NON_NULL);
configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
enable(SerializationFeature.INDENT_OUTPUT);
}
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Provisioning API")
.version("0.0.1")
.build();
}
}
控制器
@RestController
@EnableAutoConfiguration
@CrossOrigin
public class RecursoController {
@Autowired
private Configuration configuration;
@Autowired
private TypeSpecService typeSpecService;
@Autowired
private IoTAgentService ioTAgentService;
@Autowired
private OrionService orionService;
@Autowired
private DeviceIdService deviceIdService;
@ApiOperation(value = "Put a device", nickname = "provisionDevice", tags = "Device")
@ApiResponses({
@ApiResponse(code = 200, message = "Ok", response = NewDeviceResponse.class)
})
@RequestMapping(method = RequestMethod.PUT, value = "/devices", consumes = "application/json", produces = "application/json")
public ResponseEntity<NewDeviceResponse> provisionDevice(@RequestBody NewDeviceRequest newDeviceRequest,
@RequestHeader("X-Auth-Token") String oAuthToken) {
// what my method does
}
文档会产生以下swagger.json
文件:
{
swagger: "2.0",
info: {
version: "0.0.1",
title: "Provisioning API"
},
host: "localhost:8080",
basePath: "/"
}
如您所见,它只包含API的名称和版本,但不包含provisionDevice
方法。
我已经尝试了一切,但我无法弄明白我做得不好。我错过了什么?
答案 0 :(得分:0)
您是否在班级中添加了@Api注释,您的主要服务在哪里?