【发布时间】:2020-02-23 07:31:49
【问题描述】:
我在我的 Spring Boot WebFlux 应用中注意到映射与 MVC 不同。
在 MVC 中,我可以添加一个RestController("/prefix"),这样我的方法都会以/prefix 为前缀。但在 WebFlux 中,这是行不通的。
这是设计的吗?
注意 - 我换掉了 Mono<> 返回类型,看看它是否有所不同,以防这里有一些其他的自省。
例如,这是控制器-
@RestController("/api/user")
public class UserController {
private UserService service;
public UserController(UserService service) {
this.service = service;
}
@GetMapping("/me")
public Map<String, Object> whoami(@AuthenticationPrincipal OAuth2User oauth2User) {
HashMap<String, Object> response = new HashMap<>();
if (null != oauth2User) {
response.put("userName", oauth2User.getName());
response.put("userAttributes", oauth2User.getAttributes());
return response;
} else {
response.put("userName", "anonymous");
return response;
}
}
...
我认为关于依赖关系的评论是正确的 - 其中之一是引发冲突。
dependencies {
implementation project(":shared:domain")
implementation 'org.springframework.cloud:spring-cloud-starter-gateway',
'org.springframework.cloud:spring-cloud-starter-security',
'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client',
'org.springframework.boot:spring-boot-starter-webflux',
'org.springframework.boot:spring-boot-starter-oauth2-client',
'org.zalando:problem-spring-webflux:0.25.0',
"com.okta.sdk:okta-sdk-api:${oktaVersion}"
runtime "com.okta.sdk:okta-sdk-impl:${oktaVersion}",
"com.okta.sdk:okta-sdk-httpclient:${oktaVersion}"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
【问题讨论】:
-
你能分享你的依赖吗?看来你们之间可能有冲突。
-
请不要发布您的代码图片。
-
您不能添加带有
@RestController("/some/thing/here")的前缀。@RestController是专门的@Controller,这是一个@Component。@Component采用名称。因此,您现在已经为控制器分配了/api/user的 bean-name 而不是 URL 前缀。如果这是你想要的,你需要添加一个@RequestMapping("/api/user")。 -
更喜欢使用路由器功能而不是控制器。有关更多详细信息,请参阅此帖子stackoverflow.com/questions/47092029/…
标签: spring-boot spring-mvc spring-webflux