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
Comment From: snicoll
@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.
Comment From: NicoStrecker
@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?
Comment From: snicoll
You can add that in the readme.
Comment From: NicoStrecker
@snicoll you find the zip file in the updated description
Comment From: snicoll
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.
Comment From: NicoStrecker
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.
Comment From: snicoll
@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.
Comment From: NicoStrecker
Do you have a pom for me and a bean config xml i am just used to spring boot or is there a milestone i could use instead
Comment From: NicoStrecker
@snicoll So with Spring Core set to 6.1.7-SNAPSHOT
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.7-SNAPSHOT</version>
</dependency>
and the repository set to
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
It still doesnt work.
The Request in the example still returns:
Location: http://localhost:8080/hello-interface/%7Bname%7D?name=loves
instead of Location: http://localhost:8080/hello-interface/loves
Comment From: snicoll
I am not sure why you chose to upgrade only one dependency but the change isn't there.
Try setting <spring-framework.version>6.1.7-SNAPSHOT</spring-framework.version>
in the properties
section of the build. I just tried and it works.
Comment From: NicoStrecker
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