当前位置: 首页 > 知识库问答 >
问题:

带有接口和控制器实现的swagger-ui复制endpoint

洪英豪
2023-03-14

我有一个Spring启动应用程序。我选择将控制器实现为定义endpoint及其相应实现的接口(即EndpointX、EndpointXController W/EndpointXController是实现)。我在接口文件中有对swagger的所有注释,以防止实现类的混乱;但是,我在swagger UI上看到重复的endpoint,如下所示:

这是我的文档设置:

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))
            .paths(PathSelectors.ant("/consent/*"))
            .build()
            .directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
            .directModelSubstitute(java.time.OffsetDateTime.class, java.util.Date.class)
            .apiInfo(apiInfo());
}

如何告诉Swagger/Swagger-UI只显示rest服务的一个endpoint?也就是说,同意-API-Controller不会被大摇大摆地展示或拿起。

编辑:张贴的控制器和接口代码

@Controller
public class ConsentApiController implements ConsentApi {

@Autowired
private IConsentApiService consentApiService;

private final ObjectMapper objectMapper;

private final HttpServletRequest request;

@Autowired
public ConsentApiController(ObjectMapper objectMapper, HttpServletRequest request) {
    this.objectMapper = objectMapper;
    this.request = request;
}

@Override
public Optional<ObjectMapper> getObjectMapper() {
    return Optional.ofNullable(objectMapper);
}

@Override
public Optional<HttpServletRequest> getRequest() {
    return Optional.ofNullable(request);
}

public ResponseEntity getConsent(@ApiParam(value = "Identifier for the consent object to be retrieved", required = true) @Valid @RequestBody ConsentReadRequestParent consentReadRequestParent) {
    return consentApiService.getConsent(consentReadRequestParent);
}

public ResponseEntity postConsent(@ApiParam(value = "Populated consent object") @Valid @RequestBody ConsentParentRequest consentObj) {
    // Pass request to service where it will be split into DTOs and passed to DAOs and get response
    return consentApiService.postConsent(consentObj);
}

public ResponseEntity searchConsent(@Valid @RequestBody SearchParentRequest spr){
    return consentApiService.searchConsent(spr);
}



}


@Api(value = "consent")
public interface ConsentApi {

default Optional<ObjectMapper> getObjectMapper() {
    return Optional.empty();
}

default Optional<HttpServletRequest> getRequest() {
    return Optional.empty();
}

default Optional<String> getAcceptHeader() {
    return getRequest().map(r -> r.getHeader("Accept"));
}

@ApiOperation(value = "The Read Consent API is a resource that conforms to a RESTful syntax to retrieve the details of a single consent record.", nickname = "getConsent", notes = "Cannot read without parameters. Minimum of 2 characters in each field. Maximum of 50 characters in each field. Should be able to handle special characters.", response = Consent.class, tags = {"consent",})
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = Consent.class),
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 405, message = "Method Not Allowed"),
        @ApiResponse(code = 500, message = "Internal Server Error"),
        @ApiResponse(code = 604, message = "Could Not Retrieve Data for Consent"),
        @ApiResponse(code = 714, message = "Consent Not Found Matching Input Values")})
@RequestMapping(value = "/consent/read",
        method = RequestMethod.POST,
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> getConsent(@ApiParam(value = "Identifier for the consent object to be retrieved.", required = true) @Valid @RequestBody ConsentReadRequestParent consentReadRequestParent);

@ApiOperation(value = "The Create Consent API is a resource that conforms to a RESTful syntax to persist a single consent record.", nickname = "postConsent", notes = "<business and backend logic/requirements>", response = ConsentResponseParent.class, tags = {"consent",})
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = ConsentResponseParent.class),
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 405, message = "Method Not Allowed"),
        @ApiResponse(code = 500, message = "Internal Server Error")})
@RequestMapping(value = "/consent/create",
    method = RequestMethod.POST,
    consumes = {MediaType.APPLICATION_JSON_VALUE},
    produces = {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> postConsent(@ApiParam(value = "filled-out consent object")  @Valid @RequestBody ConsentParentRequest consentObj);

@ApiOperation(value = "The Search Consent API is a resource that conforms to a RESTful syntax to query consent records.", response = SearchParentResponse.class, tags = {"consent",})
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = SearchParentResponse.class),
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 405, message = "Method Not Allowed"),
        @ApiResponse(code = 500, message = "Internal Server Error")})
@RequestMapping(value = "/consent/search",
        method = RequestMethod.POST,
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> searchConsent(@Valid @RequestBody SearchParentRequest spr);

}

共有1个答案

司空健
2023-03-14

@apioperation注释中删除属性“tags”添加标记将使api显示在上下文菜单下以及它自己单独的菜单下。

 类似资料:
  • 我想制作一个实现比较器接口的比较器类。在类中,我想根据它们的ID比较我的2个员工对象。到目前为止,我制作的员工类是: 我完全被困住了,不知道该怎么办。有人能帮忙吗?

  • 问题内容: 有没有理由不将Controller映射为接口? 在所有的示例和问题中,我看到了周围的控制器,都是具体的类。是否有一个原因?我想将请求映射与实现分开。但是,当我尝试在具体类中获取a 作为参数时,我碰壁了。 我的Controller界面如下所示: And the implementing class: 该方法效果很好;在抛出一个异常 如果我将注释添加到具体类中,那么一切都会按预期工作,但是

  • ...Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers. 控制器作为应用程序逻辑的处理入口,它会负责去调用你已经实现的一些服务。通常,一个控制器会接收并解析用户的请求,然后把它转换成一个模型交给视图,由视图渲染出页面最终呈现

  • 目前,我正在使用一个使用开放Api规范的api,该规范是使用斯瓦格生成的。在 API 中有多个终结点,例如 。这会导致代码生成生成一个客户端,该客户端仅使用数字列出方法。 最好有一个名为和。(或任何区别他们的东西) (如何)使用我的nswag配置实现这一点?我也可以改变OpenApi json的生成 我的 nswag 文件如下所示: 我的swagger文件看起来像这样

  • 问题内容: 我有一个抽象的父视图,该视图旨在与其嵌套视图共享一个控制器。 路由工作正常。 问题是,当我从其中一个嵌套视图更新变量时,更改未反映在视图中。当我从父视图执行相同操作时,它可以正常工作。这种情况不需要。 我的猜测是正在为每个视图创建一个新的实例,但是我不确定为什么或如何修复它。 问题答案: 这里的问题与以下问答有关:如何在angularjs ui-router中的状态之间共享$scope

  • 我目前在我的项目中使用swagger,我有100多个控制器。我想由于大量的控制器,swagger UI文档页面需要超过5分钟来加载它的控制器。是否可以在UI页面选择特定的控制器,并只为它们加载选项?或者还有其他方法可以更快地加载UI页面?帮帮我!