0

I've noticed in my Spring Boot WebFlux app, that the mapping is different than MVC.

In MVC, I could add a RestController("/prefix") so that my methods would all be prefixed with /prefix. But in WebFlux, this doesn't work.

Is this by design?
Note - I swapped out the Mono<> return type to see if it made a difference, in case there was some other introspection being done here.

For example, this is the controller -

@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;
        }
    }
...

And the mapping is not found where I'd expect it. enter image description here

Although, it's found here - enter image description here

I think the comment about dependencies right - One of these is pulling in a conflict.

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'
}
Jeremy
  • 2,970
  • 1
  • 26
  • 50
  • 1
    Could you please share your dependencies? It seems like you might have clashes between them. – Jonathan JOhx Oct 27 '19 at 23:08
  • 1
    Please dont post pictures of your code. – Toerktumlare Oct 27 '19 at 23:08
  • 1
    You cannot add a prefix with `@RestController("/some/thing/here")`. `@RestController` is specialized `@Controller` which is an `@Component`. `@Component` takes a **name**. Hence you have now assigned a bean-name of `/api/user` to your controller NOT a URL prefix. If that is what you want you need to add an `@RequestMapping("/api/user")`. – M. Deinum Oct 28 '19 at 09:48
  • 1
    prefer using router functions instead of controllers. For more details see this post https://stackoverflow.com/questions/47092029/difference-between-controller-and-routerfunction-in-spring-5-webflux/57228341#57228341 – Serhii Povísenko Oct 28 '19 at 12:11

1 Answers1

2

@RestController("/api/user") doesn't assign a URL prefix it assigns a given name to your controller. See the javadoc. The @RestController is a specialization of the @Controller stereotype annotation, which is an @Component. The @Component takes a value and that value is to be used as a name for the given component. If no name is given the name will be auto-generated.

If you want a prefix you need an additional @RequstMapping("/api/user") on the controller class.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224