gpt4 book ai didi

java - Spring Data Rest @RepositoryRestController 和 @RequestMapping

转载 作者:行者123 更新时间:2023-12-02 11:58:12 25 4
gpt4 key购买 nike

我想使用设置了 SDR 基本路径的 @RepositoryRestController 覆盖 @RepositoryRestResource 自动生成的 Controller 方法 到“/api”。

Spring Data Rest 3.0(及更早版本)说:

“此 Controller [如代码片段中所示]将从 RepositoryRestConfiguration.setBasePath 中定义的相同 API 基本路径提供服务,该路径由所有其他 RESTful 端点(例如/api)使用”。 https://docs.spring.io/spring-data/rest/docs/3.0.1.RELEASE/reference/html/#customizing-sdr.overriding-sdr-response-handlers (第15.4章)

不过,此代码片段在类级别上没有 @RequestMapping

我的 SDR 应用程序配置了 RepositoryRestConfiguration 对象

config.setBasePath("/api");

@RepositoryRestController 不会覆盖 SDR 自动生成的 Controller 方法。

请考虑对这篇文章的接受答案: Spring Data Rest controllers: behaviour and usage of @BasePathAwareController, @RepositoryRestController, @Controller and @RestController

请帮我理解这一点! :)

AppConf.java:

@Configuration
@Import(value = {DataConf.class})
@EnableWebMvc
@ComponentScan(value = "pl.mydomain.controller")
public class AppConf
{
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
return new RepositoryRestConfigurerAdapter() {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setBasePath("/api");
}
};
}
}

TokenController.java:

@RepositoryRestController
public class TokenController
{
private TokenRepository repository;

@Autowired
public TokenController(TokenRepository tokenRepository) {
this.repository = tokenRepository;
}

@RequestMapping(method = GET, path = "/tokens")
public @ResponseBody ResponseEntity<?> tokens()
{
return ResponseEntity.ok("Hello");
}
}

TokenRepository.java:

@RepositoryRestResource(path = "tokens")
public interface TokenRepository extends CrudRepository<Token, Long>{
}

最佳答案

解决上述困境的关键是以正确的方式配置项目。也就是说,将 @ComponentScan 放在传递给 AbstractAnnotationConfigDispatcherServletInitializer::getServletConfigClasses() 方法的类中(不在传递给 getRootConfigClasses() 的 AppConf.java 中)。

DispatcherConf.java:

public class DispatcherConf extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {AppConf.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebConf.class}; // !!!
}

@Override
protected String[] getServletMappings() {
return new String[] {"/*"};
}
}

AppConf.java:

@Configuration
@Import({DataConf.class})
public class ApplicationConf
{
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setBasePath("/api"); // !!!
}
};
}
}

DataConf.java:

@Configuration
@EnableJpaRepositories(basePackages = {
"pl.example.data.repository"
})
@EnableTransactionManagement
public class DataConf
{ ... }

WebConf.java:

@Import(RepositoryRestMvcConfiguration.class)
@ComponentScan({"pl.example.api.controller"}) // !!!
public class WebConf {
}

即使我解决了这个谜题,我也不明白为什么这是一个问题。而是https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html状态:

Annotation Type ComponentScan onfigures component scanning directives for use with @Configuration classes.

关于java - Spring Data Rest @RepositoryRestController 和 @RequestMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com