After calling URL "/article/1234abcd" how to retrieve the value `1234abcd` from inside the `article` action? - spring

#RequestMapping(value = "/article", method = RequestMethod.GET)
public final String article(final ModelMap model)
{
}
If this is called using the address:
/article/1234abcd
How can the value 1234abcd be retrieved from inside the article method?

By using #PathVariable:
#RequestMapping(value = "/article/{articleId}", method = RequestMethod.GET)
public final String article(final ModelMap model, #PathVariable String articleId)
{
}
See Spring docs for more info.

Related

#PathVariable with slashes in middle of RequestMapping URL

I have a Spring controller mapping like following:
#RequestMapping(value = "api/{pathVar1}/receipt", method = RequestMethod.POST)
#ResponseBody
public String generateReceipt(HttpServletRequest request, #PathVariable String pathVar1) {
....
}
In this case what if the pathVar1 has slash('/').
Example request:
'api/CODE/1/receipt'
pathVar1 is supplied with
'CODE/1'
.
I guess it's not the cleanest solution, but it seems to be working:
Replace #PathVariable in API path with ** to accept anything between "api/" and "/receipt", and then extract the needed part of path.
#RequestMapping(value = "api/**/receipt", method = RequestMethod.POST)
#ResponseBody
public String generateReceipt(HttpServletRequest request) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String apiPath = new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
String neededPathVariableValue = apiPath.substring(0, apiPath.length() - "/receipt".length());
//...
}

What should I do in springboot if I use a post request with only one parameter?

No use of map and bean, what is a good solution?
How to get the orderId better
#RequestMapping(value = "/submitTail", method = RequestMethod.POST)
#ResponseBody
public Object generateTailOrder(String orderId) {
}
Use PathVariable if you can add orderId somewhere in URL. Take care of API standards.
Example: /orders/{orderId}/submitTrail
By doing this your API will be more informative and should not be invoked without orderId.
#RequestMapping(value = "/submitTail/{orderId}", method =
RequestMethod.GET)
public Object generateTailOrder(#PathVariable String orderId) {
}
OR
#GetMapping(value = "/submitTail/{orderId}")
public Object generateTailOrder(#PathVariable String orderId) {
}

Spring: How can I get value from uri?

How can the callbackFacebook function get the value of code from the uri?
uri = http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo
#RequestMapping(value = "/callback?{code}", method = RequestMethod.GET)
public String callbackFacebook(Model model, #PathVariable(name = "code") String code) {
System.out.println(code);
return "login";
}
Try this. code is a query parameter judging by your URL, not a path variable. Path variables are a part of the path itself (i.e. if your URL was something like /{code}/callback, then code is a PathVariable).
#RequestMapping(value = "/callback", method = RequestMethod.GET)
public String callbackFacebook(Model model, #RequestParam(value = "code") String code) {
System.out.println(code);
return "login";
}
If your URL is http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo then it is case of request parameters so the method will be like below.
#RequestMapping(value = "/callback", method = RequestMethod.GET)
public String callbackFacebook(Model model, #RequestParam(value = "code") String code) {
return "login";
}
If your URL is http://localhost:8081/callback/AQDNm6hezKdTsId5k4oXKNo then then it is case of path variables method will be like below.
#RequestMapping(value = "/callback/{code}", method = RequestMethod.GET)
public String callbackFacebook(Model model, #PathVariable(value = "code") String code) {
return "login";
}
Refer requestparam-vs-pathvariable for better clarity.
I will explain 2 ways.
1-If it is added in the session in somewhere in the project as attribute,You can get it like this :
#RequestMapping(value = "/callback?{code}", method = RequestMethod.GET)
public String callbackFacebook(Model model, #PathVariable(name = "code") String code,HttpServletRequest request) {
String code1 = request.getSession().getAttribute("code").toString();
return "login";
}
example output : AQDNm6hezKdTsId5k4oXKNo
2-You can directly get URL.But then you need to parse URL.Because all URL is coming.
#RequestMapping(value = "/callback?{code}", method = RequestMethod.GET)
public String callbackFacebook(Model model, #PathVariable(name = "code") String code,HttpServletRequest request) {
StringBuffer requestURL = request.getRequestURL();
return "login";
}
example output : http://localhost:8081/callback?code=AQDNm6hezKdTsId5k4oXKNo

I need to pass two #RequestBody parameters into my REST API POST method. How can I achieve this and can I do it using DefaultHttpClient?

My REST API method is:
#RequestMapping(value = "/api/test/summary", method = RequestMethod.POST)
postSummaryData(#RequestBody String one, #RequestBody String two) { ... }
#RequestMapping(value = "/api/test/summary/{one}/{two}", method = RequestMethod.POST) postSummaryData( #RequestParam String one, #RequestParam String two) {
}
Also you can try below:
#RequestMapping(value = "/api/test/summary", method = RequestMethod.POST) postSummaryData() {
RequestAttributes attr = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes)attr).getRequest();
// you can get param from this request
}

How can i send get method's data to post method without using view?

Suppose, I have a value/data(in my exaple name) in the get method.I want to send and access it in the post method without using view.How can I do that?
#RequestMapping(value="/edit",method = RequestMethod.GET)
public String getPerson(ModelMap model,Person person){
String name="Person Name";
return "personEdit";
}
#RequestMapping(value="/save",method = RequestMethod.POST)
public String savePerson(ModelMap model,Person person){
//I want to access/get **name** here
return "details";
}
you can use modelMap to send the value
#RequestMapping(value="/edit",method = RequestMethod.GET)
public String getPerson(ModelMap model,Person person){
String name="Person Name";
model.addAttribute("name","Person Name");
return "personEdit";
}
you can access your value by using JSTL forexmp : ${name} ..

Resources