Skip to content

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

@Nico-Strecker

Description

@Nico-Strecker

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

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions