user8453102
user8453102

Reputation:

Access absolute path of resource when using @RequestMapping in spring boot controller

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.

Upvotes: 0

Views: 1905

Answers (4)

Tarasare
Tarasare

Reputation: 27

@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(){};

Upvotes: 0

user8453102
user8453102

Reputation:

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

Upvotes: 1

surya
surya

Reputation: 2749

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}"

Upvotes: 0

Codetector
Codetector

Reputation: 774

@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)

Upvotes: 0

Related Questions