Spring @PathVariable and @RequestMapping.

Asked 2 years ago, Updated 2 years ago, 68 views

Hello, seniors...

I use Spring, and I have a question about PathVariable and RequestMapping.

@RequestMapping(value="/web/service/{serviceName}")
public ModelAndView webService(
        @PathVariable("serviceName") String serviceName,
        ModelMap map) {
          .....
@RequestMapping(value="/web/service/test")
public ModelAndView test(
        ModelMap map) {
          .....

When there are two together in the class,

Call to localhost:8080/web/service/test.

I wonder if you always go with the test function.

When I tested it by myself, I think it always goes to the test function

I don't know if I should use it because there's no guarantee that it always goes to the test function.

java spring

2022-09-22 20:45

1 Answers

Always call the test method when calling localhost:8080/web/service/test.

Instead, if a value other than test is entered, the webService method is called.

When looking at the code you wrote, the difference is ambiguous when looking at the resource (URI) only with your eyes, so I recommend you give it a depth or call it according to the role by dividing it by http methods.

REST API Note and examples

Delete member information: @DELETE/members/{id}

Acquisition of membership information: @GET/members/{id}

Enter member information: @POST/members/{id}

Obtaining information from all members: @GET/members


2022-09-22 20:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.