Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MvcUriComponentsBuilder.fromMethodName does not pick the annotated method transparently #32553

Closed
NicoStrecker opened this issue Mar 30, 2024 · 11 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@NicoStrecker
Copy link

NicoStrecker commented Mar 30, 2024

Version: `Spring-Boot 3.2.4

I try to use the MvcUriComponenntsBuilder.fromMethodName to generate the path to a ressource based on a method of my controller.

return ResponseEntity.created(MvcUriComponentsBuilder.fromMethodName(this.getClass(), "getMember", id)
                                              .build()
                                              .toUri()).build();

My Controller is implementing an interface where the API is defined.

public class MemberController implements MemberApi { ...

the getMember-Method looks like this:

    @Override
    public ResponseEntity<MemberDto> getMember(Integer memberId) {
        return ResponseEntity.ok(memberService.getMember(memberId));
    }

and in the interface like this:

    @GetMapping(value="/api/v1/members/{member-id}")
    @Operation(description = "Get all members")
    @ApiResponse(responseCode = "200", description = "The list of all members was requested sucessfully.")
    default ResponseEntity<MemberDto> getMember(@PathVariable("member-id") Integer memberId){
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

Now i get the following behaviour, when i use the this.getClass() argument my location-header looks like this:

Location: http://localhost:8080/api/v1/members/%7Bmember-id%7D?memberId=12

When i use the MemberApi.class it looks like this:

Location: http://localhost:8080/api/v1/members/12

UPDATE 10.04.2024:
Spring-Issue-MvcUriCompontesBuilder-Example.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 30, 2024
@snicoll snicoll added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Mar 30, 2024
@snicoll
Copy link
Member

snicoll commented Apr 4, 2024

@NicoStrecker rather than code in text, can you please edit your description and replace it with a sample application we can run ourselves. You can attach a zip to this issue or push the code the a GitHub repository.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Apr 4, 2024
@NicoStrecker
Copy link
Author

@NicoStrecker rather than code in text, can you please edit your description and replace it with a sample application we can run ourselves. You can attach a zip to this issue or push the code the a GitHub repository.

Sure should i add comments for expected behaviour?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 5, 2024
@snicoll
Copy link
Member

snicoll commented Apr 6, 2024

You can add that in the readme.

@snicoll snicoll added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Apr 6, 2024
@NicoStrecker
Copy link
Author

@snicoll you find the zip file in the updated description

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 10, 2024
@snicoll
Copy link
Member

snicoll commented Apr 10, 2024

Thanks Nico. This seems to be due to the fact that the @PathVariable isn't detected. The code in your sample doesn't provide the annotated type, using MvcUriComponentsBuilder.fromMethodName(HelloWorldApi.class, "helloNameInterface", name) works as expected.

We'll see what we can do to transparently handle this case.

@snicoll snicoll changed the title MvcUriComponentsBuilder.fromMethodName does not work when Interface is used MvcUriComponentsBuilder.fromMethodName does not pick the annotated method transparently Apr 10, 2024
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Apr 10, 2024
@snicoll snicoll added this to the 6.1.x milestone Apr 10, 2024
@NicoStrecker
Copy link
Author

Thank you very much. We frequently define our REST interface in an "API" interface with Springdoc, where we define our path variables and implement it in the class without the many annotations.

@snicoll snicoll self-assigned this Apr 23, 2024
@snicoll snicoll modified the milestones: 6.1.x, 6.1.7 Apr 23, 2024
@snicoll
Copy link
Member

snicoll commented Apr 23, 2024

@NicoStrecker if you get a chance to test 6.1.7-SNAPSHOT before it's released, and this job completes, that would be very much appreciated.

@NicoStrecker

This comment was marked as resolved.

@NicoStrecker

This comment was marked as resolved.

@snicoll

This comment was marked as resolved.

@NicoStrecker
Copy link
Author

Sorry @snicoll i just didnt know how to do that. I have to say there is not much information about spring-framework.version property, when searching the internet.

I tried it out and it works as expected thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants