I'm generating controller interfaces with openapi-generator-maven-plugin
. The code is generated succesfully and I create controllers that implement generated interfaces.
But the problem is that Spring doesn't see these controllers at all.
My pom.xml with plugin config:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<id>generate-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/openapi/notification-settings/notification-settings-controller.yaml
</inputSpec>
<generatorName>spring</generatorName>
<library>spring-boot</library>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateModels>true</generateModels>
<generateModelTests>false</generateModelTests>
<apiPackage>com.epam.stts.api</apiPackage>
<modelPackage>com.epam.stts.model</modelPackage>
<importMappings>
ChannelType=com.epam.stts.type.ChannelType,
NotificationSettingsType=com.epam.stts.type.NotificationSettingsType,
Period=com.epam.stts.component.Period
</importMappings>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<serializableModel>true</serializableModel>
<skipDefaultInterface>true</skipDefaultInterface>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<useOptional>false</useOptional>
<additionalModelTypeAnnotations>
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
@lombok.Builder
</additionalModelTypeAnnotations>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
And generated interface is:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Tag(name = "notification-settings", description = "the notification-settings API")
public interface NotificationSettingsApi {
/**
* GET /notification-settings : Get user's notification settings
*
* @param userId ID of user (required)
* @return OK (status code 200)
*/
@Operation(
operationId = "getUserNotificationSettings",
summary = "Get user's notification settings",
responses = {
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = NotificationSettingsInfo.class))
})
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/notification-settings",
produces = { "application/json" }
)
ResponseEntity<List<NotificationSettingsInfo>> getUserNotificationSettings(
@Parameter(name = "userId", description = "ID of user", required = true) @PathVariable("userId") Long userId
);
/**
* POST /notification-settings/save : Save notification settings
*
* @param notificationSettingsInfo NotificationSettingsInfo request body (optional)
* @return OK (status code 200)
* or Validation exception while mapping request body (status code 400)
*/
@Operation(
operationId = "saveNotificationSettings",
summary = "Save notification settings",
responses = {
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Object.class))
}),
@ApiResponse(responseCode = "400", description = "Validation exception while mapping request body", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = Object.class))
})
}
)
@RequestMapping(
method = RequestMethod.POST,
value = "/notification-settings/save",
produces = { "application/json" },
consumes = { "application/json" }
)
ResponseEntity<Object> saveNotificationSettings(
@Parameter(name = "NotificationSettingsInfo", description = "NotificationSettingsInfo request body") @Valid @RequestBody(required = false) NotificationSettingsInfo notificationSettingsInfo
);
}
Controller just implements the interface and makes calls to actual service etc. But when I start the application (or perform any MVC test) I just get HTTP 404 while trying to make some requests to this controller.
I have no idea of what is wrong here and I can't figure out what is missing.
Will be grateful for any help.
UPDATE:
I figured it out. Added additional <useSpringController>
tag to plugin configuration so generated interfaces are now annotated with @Controller
.
And now it works.
This tag <interfaceOnly>true</interfaceOnly>
prevents openapi-generator generating controllers.