Swagger 不会添加新的 api 控制器来休息(Spring 启动)
Swagger doesn't add new api controllers to rest (Spring boot)
Swagger 无法识别新的 rest 接口 api。
这是swagger配置文件。
package trn06.administracion.api.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Slf4j
@EnableSwagger2
@Configuration
public class TRN06SwaggerConfiguration {
@Value("${custom.host}")
private String hostValue;
private ApiInfo _apiInfo(final String version) {
return new ApiInfoBuilder()
.title("Administrative events")
.termsOfServiceUrl("")
.version(version)
.contact(new Contact("",
"",
""))
.build();
}
private Docket _configureVersion(final String version) {
// Get environment from java environment variables
return new Docket(DocumentationType.SWAGGER_2)
.host(hostValue)
.groupName("Version_" + version)
.select()
.apis(RequestHandlerSelectors.basePackage("trn06.administracion.api.controller.rest"))
.paths(PathSelectors.ant("/v" + version + "/**"))
.build()
.useDefaultResponseMessages(false)
.apiInfo(_apiInfo(version));
}
@Bean
Docket configureV1_0() {
return _configureVersion("1.0");
}
}
包 trn06.administracion.api.controller.rest 上定义的所有接口都可以,事实上我还有两个其他接口映射正常,除了这个:
package trn06.administracion.api.controller.rest;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import trn06.administracion.api.model.dto.TRN06ErrorDto;
import trn06.administracion.api.model.dto.TRN06EventsAuthorityDto;
import trn06.administracion.api.model.dto.TRN06ValidationErrorDto;
import java.util.List;
@Api(value = "authorities", description = "Event's authorities ", tags = "Authorities")
public interface TRN06EventsAuthorityApiController {
@ApiOperation(value = "List of authorities", nickname = "findAuthorities", response = TRN06EventsAuthorityDto.class, responseContainer = "List", tags = {"Authorities,"})
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Operation performed", response = TRN06EventsAuthorityDto.class, responseContainer = "List"),
@ApiResponse(code = 400, message = "Validation error", response = TRN06ValidationErrorDto.class),
@ApiResponse(code = 404, message = "Not found"),
@ApiResponse(code = 500, message = "Error", response = TRN06ErrorDto.class)})
@GetMapping(value = "/v1.0/authorities",
produces = "application/json")
List<TRN06EventsAuthorityDto> findAuthorities();
}
看起来像数据库实体没问题,我尝试出错并被正确检测到。尝试将 RequestHandlerSelectors 更改为 any(),结果相同。
不知道,也不知道发生了什么。
亲切的问候
您需要创建该接口的实现,并在实现中添加@RestController和@RequestMapping注解
例如:
@RestController
@RequestMapping("/findAuthorities")
public class TRN06EventsAuthorityApiControllerImpl implements TRN06EventsAuthorityApiController {
@Override
public List<String> findAuthorities() {
return null;
}
}
就我而言,是我忘记的注释
public class TRN06EventsTypesApiControllerImpl
implements TRN06EventsTypesApiController {
....
添加注解@RestController 后就可以了
@RestController
public class TRN06EventsTypesApiControllerImpl
implements TRN06EventsTypesApiController {
....
不管怎样,我注意到了@RequestMapping注解。
感谢@v-mokrecov和@mahesh-loya
Swagger 无法识别新的 rest 接口 api。
这是swagger配置文件。
package trn06.administracion.api.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Slf4j
@EnableSwagger2
@Configuration
public class TRN06SwaggerConfiguration {
@Value("${custom.host}")
private String hostValue;
private ApiInfo _apiInfo(final String version) {
return new ApiInfoBuilder()
.title("Administrative events")
.termsOfServiceUrl("")
.version(version)
.contact(new Contact("",
"",
""))
.build();
}
private Docket _configureVersion(final String version) {
// Get environment from java environment variables
return new Docket(DocumentationType.SWAGGER_2)
.host(hostValue)
.groupName("Version_" + version)
.select()
.apis(RequestHandlerSelectors.basePackage("trn06.administracion.api.controller.rest"))
.paths(PathSelectors.ant("/v" + version + "/**"))
.build()
.useDefaultResponseMessages(false)
.apiInfo(_apiInfo(version));
}
@Bean
Docket configureV1_0() {
return _configureVersion("1.0");
}
}
包 trn06.administracion.api.controller.rest 上定义的所有接口都可以,事实上我还有两个其他接口映射正常,除了这个:
package trn06.administracion.api.controller.rest;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import trn06.administracion.api.model.dto.TRN06ErrorDto;
import trn06.administracion.api.model.dto.TRN06EventsAuthorityDto;
import trn06.administracion.api.model.dto.TRN06ValidationErrorDto;
import java.util.List;
@Api(value = "authorities", description = "Event's authorities ", tags = "Authorities")
public interface TRN06EventsAuthorityApiController {
@ApiOperation(value = "List of authorities", nickname = "findAuthorities", response = TRN06EventsAuthorityDto.class, responseContainer = "List", tags = {"Authorities,"})
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Operation performed", response = TRN06EventsAuthorityDto.class, responseContainer = "List"),
@ApiResponse(code = 400, message = "Validation error", response = TRN06ValidationErrorDto.class),
@ApiResponse(code = 404, message = "Not found"),
@ApiResponse(code = 500, message = "Error", response = TRN06ErrorDto.class)})
@GetMapping(value = "/v1.0/authorities",
produces = "application/json")
List<TRN06EventsAuthorityDto> findAuthorities();
}
看起来像数据库实体没问题,我尝试出错并被正确检测到。尝试将 RequestHandlerSelectors 更改为 any(),结果相同。
不知道,也不知道发生了什么。
亲切的问候
您需要创建该接口的实现,并在实现中添加@RestController和@RequestMapping注解
例如:
@RestController
@RequestMapping("/findAuthorities")
public class TRN06EventsAuthorityApiControllerImpl implements TRN06EventsAuthorityApiController {
@Override
public List<String> findAuthorities() {
return null;
}
}
就我而言,是我忘记的注释
public class TRN06EventsTypesApiControllerImpl
implements TRN06EventsTypesApiController {
....
添加注解@RestController 后就可以了
@RestController
public class TRN06EventsTypesApiControllerImpl
implements TRN06EventsTypesApiController {
....
不管怎样,我注意到了@RequestMapping注解。
感谢@v-mokrecov和@mahesh-loya