Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC @Path variable with { } braces

Am developing an application using spring boot. In REST controller i prefer to use path variable(@PathVariabale annotation). My code fetching the path variable but it contatins { } braces as it is in the url. Please any one suggest me to solve this issue

@RequestMapping(value = "/user/item/{loginName}", method = RequestMethod.GET)
public void getSourceDetails(@PathVariable String loginName) {
    try {
        System.out.println(loginName);
        // it print like this  {john}
    } catch (Exception e) {
        LOG.error(e);
    }
}

URL

http://localhost:8080/user/item/{john}

Out put in controller

{john}

like image 754
Aravind Cheekkallur Avatar asked Nov 07 '14 04:11

Aravind Cheekkallur


People also ask

Can we use RequestParam and PathVariable together?

The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters. These annotations can be mixed together inside the same controller. @PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.

What is @path and path Param path variable?

@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call. @PathVariable is to obtain some placeholder from the URI (Spring call it an URI Template)

What is difference between @PathVariable and @RequestParam in Spring?

Difference between @PathVariable and @RequestParam in Spring 1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.

What is @path annotation in Spring boot?

The @Path annotation identifies the URI path template to which the resource responds, and is specified at the class level of a resource.


1 Answers

Use http://localhost:8080/user/item/john to submit your request instead.

You give Spring a value of "{john}" to the path variable loginName, so Spring get it with the "{}"

Web MVC framework states that

URI Template Patterns

URI templates can be used for convenient access to selected parts of a URL in a @RequestMapping method.

A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI. The proposed RFC for URI Templates defines how a URI is parameterized. For example, the URI Template http://www.example.com/users/{userId} contains the variable userId. Assigning the value fred to the variable yields http://www.example.com/users/fred.

In Spring MVC you can use the @PathVariable annotation on a method argument to bind it to the value of a URI template variable:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
 public String findOwner(@PathVariable String ownerId, Model model) {
     Owner owner = ownerService.findOwner(ownerId);
     model.addAttribute("owner", owner);
     return "displayOwner"; 
  }

The URI Template " /owners/{ownerId}" specifies the variable name ownerId. When the controller handles this request, the value of ownerId is set to the value found in the appropriate part of the URI. For example, when a request comes in for /owners/fred, the value of ownerId is fred.

like image 183
JaskeyLam Avatar answered Oct 16 '22 17:10

JaskeyLam