0

I am using @RquestMapping for mapping url to controller method.

@RestController
@RequestMapping(path = "/api/tasks")
public class TaskController { ....

and methods inside controller have /{id} with request mapping annotations.

 @RequestMapping(
    path = "/{taskId},
    method = RequestMethod.GET
)
public Map<String, Object> methodOne(...

I want to access http method and absolute path (configured path) for that method inside.

i.e. I want to get /api/tasks/{taskId} as value (not the /api/tasks/1 if api is called for task id 1) and GET as method inside of the methodOne.

I checked the HandlerMapping but it returns actual path of resource called by client. Not the configured path for the method / resource.

Any help or guidance would be highly appreciated. Thank you.

4 Answers 4

1
String[] pathReqMappingAnnotationOnControllerClass = this.getClass().getAnnotation(RequestMapping.class).path();

Method method = TaskApiController.class.getMethod("getListOfTasks", HttpServletRequest.class, HttpServletResponse.class);

String[] pathReqMappingAnnotationOnControllerMethod = method.getAnnotation(RequestMapping.class).path();

String wholePath = pathReqMappingAnnotationOnControllerClass[0] + pathReqMappingAnnotationOnControllerMethod[0];
//pathReqMappingAnnotationOnControllerMethod will be empty array if method is not annotated for path
Sign up to request clarification or add additional context in comments.

1 Comment

Please note that a code is read much more times , so using reflection are you not complicating the whole thing, you even have some string constant for method name , so why can’t you just use a constant for {taskId}
0
@RequestMapping(path = "/{id}", method = [RequestMethod.DELETE])
public void test(@PathVariable("id") String id, HttpServletRequest request) {
   \\ Code Here
}

In the method parameter, id is the pathVariable. And request method is accessible in the request variable (Although I do not know what is the point as you are limiting it to only accept GET requests)

3 Comments

Is there a way without changing the method definition? I dont have request as parameter to any of my controller methods. I have other methods to handle get/post/delete/put. I want to fetch path and method for each method and then fetch some settings for that respective call.
@RequestMapping( path = /{id}, method = RequestMethod.GET ) methodOne(@PathVariable("id") String id)
@sgrillon I am able to access path variable but i want to access the absolute path of request. that is /api/tasks/{id} which is mapped with request mapping. Right now with request object i could only get /api/tasks/1 or what ever task id is requested. I want to get the configured path.
0

As suggested by @codedetector, best option is if you have request object or you can add one if you dont have it.

@RequestMapping(path = "/{taskId}, method = RequestMethod.GET)
public String methodOne(HttpServletRequest request){
   String test = request.getRequestURI();
   return test;
 }

If you dont have request object in your method, with below code you can get any URL on your system.

import org.springframework.hateoas.mvc.ControllerLinkBuilder
 ...
ControllerLinkBuilder linkBuilder =  ControllerLinkBuilder.linkTo(methodOn(YourController.class).getSomeEntityMethod(parameterId, parameterTwoId))

URI methodUri = linkBuilder.Uri()
String methodUrl = methodUri.getPath()

--------Edit I am not sure why you need in this format "/api/tasks/{taskId}" as value (not the /api/tasks/1 )but i can think of using a constant use it for your @RequestMapping path parameter and then easily after getting absolute path , replace/append it with that constant.

String pathParam ="/{taskId}"

5 Comments

It doesnt return path configured on controller annotation. it returns `api/tasks/1 or what ever id.
which option did you try , using request object or link builder ?
api/tasks is in your controller annotation. what am i missing ?
Yes. I tried first one before but was playing around with ControllerLinkBuilder. But that also gives me actual api path not the configured one. Controller is mapped with /api/tasks and method as /{taskId} i want to get the configured path i.e. /api/tasks/{taskId}. Right now everything i tried returns /api/tasks/1
thats my fallback solution too. i have created helper bean to return the absolute path. I wanted to remove the overhead of doing it altogether thinking if there was some method that spring uses to map request mapping patterns with called url by clients. but those methods are private in request handlers.
0
@RequestMapping(path = "/{id}", method = [RequestMethod.DELETE])
public void test(@PathVariable("id") String id, HttpServletRequest request) {
    switch(id){
        case 1:
            method1();
            break;
        case 2:
            method2();
            break
        ....
        ....
    }
}
private void method1(){};
private void method2(){};
private void method3(){};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.