Swagger控制器映射

时间:2017-01-20 04:13:30

标签: java rest swagger swagger-ui swagger-2.0

使用Swagger-UI我试图在一个Swagger容器下面显示具有相同基本URL的方法,而不管它们属于哪个控制器。想象一下这段代码。

// Current Swagger Config Class
@Bean
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)  
      .select()                                  
      .apis(RequestHandlerSelectors.any())              
      .paths(getPaths())                          
      .build()
      .apiInfo(getApiInfo());                                       
}

private Predicate<String> getPaths(){
    return or(regex("/attributes.*"),
              regex("/other.*"));
}

// Controller 1
@RestController
@RequestMapping("/attributes")
@Api(value = "User Attribute Services", description = "User Attributes API")
public class UserAttributesController {

    @Autowired
    private UserAttributesService userService;
    @Autowired
    private UserAttributeValidator userAttributeValidator;

    @RequestMapping(value = "/user/search", method = RequestMethod.POST, produces = "application/json")
    public List<User> searchUser(@RequestBody List<UserDTO> userDTOList){
        // meaningless implementation ...
    }
    @RequestMapping(value = "/user/insert", method = RequestMethod.POST)
    public boolean insertUser(@RequestBody List<UserDTO> userDTOList){
        // meaningless implementation ...
    }
    @RequestMapping(value = "/user/update{id}", method = RequestMethod.PATCH)
    public boolean updateUser(@PathVariable id, @RequestBody UserDTO updateDetails){
        // meaningless implementation ...
    }
    @RequestMapping(value = "/user/delete/{id}", method = RequestMethod.DELETE)
    public boolean deleteUser(@PathVariable id){
        // meaningless implementation ...
    }
}

// Controller 2
@RestController
@RequestMapping("/attributes")
@Api(value = "Tech Attribute Services", description = "Tech Attributes API")
public class TechAttributesController {

    @Autowired
    private TechAttributesService techService;
    @Autowired
    private TechAttributeValidator techAttributeValidator;

    @RequestMapping(value = "/tech/search", method = RequestMethod.POST, produces = "application/json")
    public List<Tech> searchTech(@RequestBody List<TechDTO> techDTOList){
        // meaningless implementation ...
    }
    @RequestMapping(value = "/tech/insert", method = RequestMethod.POST)
    public boolean insertTech(@RequestBody List<TechDTO> techDTOList){
        // meaningless implementation ...
    }
    @RequestMapping(value = "/tech/update{id}", method = RequestMethod.PATCH)
    public boolean updateTech(@PathVariable id, @RequestBody TechDTO updateDetails){
        // meaningless implementation ...
    }
    @RequestMapping(value = "/tech/delete/{id}", method = RequestMethod.DELETE)
    public boolean deleteTech(@PathVariable id){
        // meaningless implementation ...
    }
}

我的应用程序招摇页面显示了单独容器下的属性。虽然我指定了&#39; / attributes&#39;的正则表达式路径。在我的Swagger配置中,这些在不同的控制器中的事实超越了我想要的。如果我将所有这些方法放入1个控制器中,我会得到所需的结果,但这只是我拥有的属性类的一个子集,我不想要一个跨越1k行的大型控制器。

额外:如果您注意到属性的服务几乎相同。我一直试图找到一种巧妙的方法来声明它们一次并简单地覆盖方法declations而不是重新定义类似的服务,但是因为@RequestMapping注释需要是一个常量值,我找不到一种方法来改变注释依赖关于实施而不是宣言。

Swagger-UI Picture

0 个答案:

没有答案