gpt4 book ai didi

java - Spring 启动 : Jersey ResourceConfig needs annotating?

转载 作者:行者123 更新时间:2023-11-29 08:35:25 24 4
gpt4 key购买 nike

我刚刚开始使用 Spring Boot,我想实现一个 ResourceConfig,但我发现了一些相互矛盾的想法。

采取以下措施

@Component
public class JerseyExampleConfig extends ResourceConfig {

上面用COMPONENT注释

@Configuration
public class JerseyExampleConfig extends ResourceConfig {

哪个是正确的?

我认为使用 Configuration 注释是正确的方法,但在示例中似乎使用了 Component。

有什么想法吗?

有什么区别?

最佳答案

documentation建议 @Component:

To get started with Jersey 2.x just include the spring-boot-starter-jersey as a dependency and then you need one @Bean of type ResourceConfig in which you register all the endpoints:

@Component
public class JerseyConfig extends ResourceConfig {

public JerseyConfig() {
register(Endpoint.class);
}
}

documentation还说了以下内容:

You can also register an arbitrary number of beans implementing ResourceConfigCustomizer for more advanced customizations.

All the registered endpoints should be @Components with HTTP resource annotations (@GET etc.), e.g.

@Component
@Path("/hello")
public class Endpoint {

@GET
public String message() {
return "Hello";
}
}

Since the Endpoint is a Spring @Component its lifecycle is managed by Spring and you can @Autowired dependencies and inject external configuration with @Value. The Jersey servlet will be registered and mapped to /* by default. You can change the mapping by adding @ApplicationPath to your ResourceConfig.

关于java - Spring 启动 : Jersey ResourceConfig needs annotating?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430581/

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