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

Spring Boot中配置的Swagger只显示带有POST和GET映射的方法

江鸿羲
2023-03-14
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("my.project.controllers"))
                .paths(PathSelectors.ant("/api/*"))
                .build();
    }
}
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
            <scope>compile</scope>
        </dependency>
@Autowired
private UserService userService;

protected UserService getService() {
    return userService;
}

@RequestMapping(method = GET)
public Page<User> query(@RequestParam Map<String, Object> parameters, Pageable pageable) {
    return getService().query(parameters, pageable);
}

@ResponseStatus(CREATED)
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<User> create(@RequestBody User entity) {
    return ResponseEntity.status(HttpStatus.CREATED).body(getService().create(entity));
}

@RequestMapping(value = "/{id:[0-9]+}", method = RequestMethod.PUT)
public ResponseEntity<User> update(@PathVariable Long id, @RequestBody User entity) {
    return ResponseEntity.ok(getService().update(id, entity));
}

@RequestMapping("/current")
public ResponseEntity current() {
    return ResponseEntity.ok(userService.getUser());
}

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id:[0-9]+}/enable", method = RequestMethod.POST)
public void enable(@PathVariable("id") final long id) {
    userService.enable(id);
}

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id:[0-9]+}/disable", method = RequestMethod.POST)
public void disable(@PathVariable("id") final long id) {
    userService.disable(id);
}

@RequestMapping(value = "/histories", method = RequestMethod.GET)
public List<UserHistory> histories() {
    return userService.histories();
}

可能是我需要添加一些更多的配置还是添加一些其他的东西?

共有1个答案

巫经义
2023-03-14

基于您的控制器,我认为您应该在您的swagger配置中的路径匹配器中再添加一个星号:

.paths(PathSelectors.ant(“/api/**”))

例如/api/users/current将不与/api/*匹配,而是与/api/**匹配,这就是为什么您只将基本路径endpoint记录在案的原因。

 类似资料:
  • 导致问题的两个endpoint位于根路径上,并且URL中只有一个path参数。它们都在一个具有@requestmapping(“/”)的控制器中,并对它们进行注释; 这两个endpoint工作正常,只是在Swagger呈现HTML时引起了一些问题。如果我删除它们,将显示HTML。我已经尝试将它们自己移动到控制器中,看看是否可以阻止swagger在swagger配置中访问它们。但是,它们似乎只需要存

  • 您好,我正在尝试创建一个POST方法,但我一直收到“404 Request方法'GET'not support”错误。下面我将发布我的Rest控制器,下面我将发布我的服务类。唯一不起作用的是@PostMaps方法。 我没有看到这个问题,我尝试切换到@GetMapping并删除了实际的事务“billRepository.delete(billToWithdraw);”然后该方法返回正确的账单。

  • 本文向大家介绍SpringBoot整合Swagger的方法示例,包括了SpringBoot整合Swagger的方法示例的使用技巧和注意事项,需要的朋友参考一下 依赖 配置类 启动类 在springBoot的启动类上添加一个注解即可配置成功: @EnableSwagger2 访问api的路径 http://ip/projectName/swagger-ui.html 注解说明 @Api 标注在类上,

  • 我对Java服务器端编程还不熟悉,我的问题基本上是使用Servlets(低级别,不使用spring mvc等)开始一个起点,然后从node开始构建。js后台,其中路由定义将以函数(,等),函数将在http方法之一(GET、post、PUT、DELETE)的参数中接收和。 如果有人可以帮助我,从一个servlet类中的路由(比如说)开始定义方法,这个servlet类映射到http方法,同时在其参数中

  • 我有这个目标: 还有这个:

  • 我们正在使用带有Spring数据的MapStruct在JPA实体和DTO类之间进行转换。所有映射器都使用方法beanToDTO()和dtoToBean()遵循相同的模式。经过一次学习治疗后,我们就有了这一切。现在我们尝试使用Spring注入来替换JPA实体、DTO和映射器类上的实现。我们有JPA实体和DTO替换工作。所以现在我们尝试让Spring注入替代映射器实现。 对于我们的问题,我们可以子类映