代码之家  ›  专栏  ›  技术社区  ›  drenda

将自定义端点添加到Spring Data REST存储库并以夸张的方式显示

  •  2
  • drenda  · 技术社区  · 7 年前

    Spring Data REST很棒,但有时我需要向特定端点添加自定义操作。不幸地 this question 没有帮助。

    我的典型存储库是:

    @Transactional
    @PreAuthorize("isAuthenticated()")
    public interface WorkSessionRepository extends PagingAndSortingRepository<WorkSession, Long> {}
    

    假设我有一个自定义控制器:

    @RepositoryRestController
    @RequestMapping(path = "/api/v1/workSessions")
    public class WorkSessionController {
    
        @Autowired
        private EntityLinks entityLinks;
    
        @Autowired
        private WorkSessionRepository workSessionRepository;
    
        @Autowired
        private UserRepository userRepository;
    
        @PreAuthorize("isAuthenticated()")
        @RequestMapping(method = RequestMethod.POST, path = "/start")
        public ResponseEntity<?> start(@RequestBody(required = true) CheckPoint checkPoint) {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        ....
        ....
        Resource<WorkSession> resource = new Resource<>(workSession); 
        resource.add(entityLinks.linkFor(WorkSession.class).slash(workSession.getId()).withSelfRel());
        return ResponseEntity.ok(resource);
    

    swagger view

    我希望我的方法添加到WorkSessionEntity中。

    这是Swagger的配置:

    @Configuration
    @EnableSwagger2
    @Import({ springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class,
            springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class })
    @ComponentScan(basePackageClasses = { BusletApplication.class })
    public class SwaggerConfig {
    
        @Autowired
        private TypeResolver typeResolver;
    
        @Bean
        public Docket internalAngularApi() {
            return new Docket(DocumentationType.SPRING_WEB)// Enable swagger2
                    .select().//
                    apis(RequestHandlerSelectors.any())// what list in the doc
                    .paths(paths()).build()// select which paths shows
                    .pathMapping("/").// the api base path
                    directModelSubstitute(LocalDate.class, String.class).// replace
                                                                            // specific
                                                                            // model
                    genericModelSubstitutes(ResponseEntity.class)// generic
                                                                    // replacement
                    .alternateTypeRules(
                            newRule(typeResolver.resolve(DeferredResult.class, typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                                    typeResolver.resolve(WildcardType.class)))
                    .useDefaultResponseMessages(false).//
                    enableUrlTemplating(false).apiInfo(apiInfo()).groupName("Internal API for Angular UI");//
        }
    
        // Here is an example where we select any api that matches one of these
        // paths
        private Predicate<String> paths() {
            return or(regex("/api/v1/.*"), regex("/error/.*"));
        }
    
        @Bean
        public UiConfiguration uiConfig() {
            return new UiConfiguration("validatorUrl", // url
                    "none", // docExpansion => none | list
                    "alpha", // apiSorter => alpha
                    "schema", // defaultModelRendering => schema
                    UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, //
                    false, // enableJsonEditor
                    true, // showRequestHeaders => true | false
                    6000L); // requestTimeout => in milliseconds
        }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Alejandro    7 年前

    使用 @Api(tags= " WorkSession Entity") 在你的 WorkSessionController