0

I want to get all parameters (not need for headers) from an httpRequest in à spring service component

I'm using Spring boot, look at this example :

private final MyService myService;

@RequestMapping(value = "/processform/{process_id}", method = RequestMethod.POST)
    public @ResponseBody
    LinkedHashMap<String, String> runForm( String process_id,
                                               @RequestParam String className,
                                               @RequestBody(required = false) IupicsFormVO vo) {
        return myService.run(process_id, className, vo);
    }

This controller generate this curl (without headers):

curl -X POST \
  'http://localhost:8087/processform/119?className=com.stackOverflow.question.ClassName.java' \
  -d '{  
"name" : "Name",
"age" : "Age"
}'

Now what i need is to get all parameters from this URL (may be with injecting HttpServletRequest )

The expected result is kind of :

{  
   "process_id":"119",
   "className":"com.stackOverflow.question.ClassName.java",
   "body":{  
      "name":"Name",
      "age":"Age"
   }
}

I found this example,

String finalPath = apm.extractPathWithinPattern(bestMatchPattern, path

but when i use'it i got always en empty finalPath Thanks for your time

2 Answers2

0

You need to put a variable in the path to use @PathVariable. For example:

@RequestMapping(value = "/processform/{id}", method = RequestMethod.POST)
public @ResponseBody
LinkedHashMap<String, String> runForm(@PathVariable("id") String process_id, ...
cmoetzing
  • 742
  • 3
  • 16
  • this is not hat i want, the controller work fine i just forgot to copy it i will update my question, but i want later to get those information from HTTPRequest – satck djallil Feb 12 '19 at 10:42
0

Your path should have path variable placeholder. /processform/{process_id}. Also you need to specify request parameter

@RequestMapping(value = "/processform/{process_id}", method = RequestMethod.POST)
    public @ResponseBody
    LinkedHashMap<String, String> runForm(HttpServletRequest request, @PathVariable("process_id") String process_id, @RequestParam("name") String lassName,@RequestParam("age") String age,
                                               @RequestBody(required = false) IupicsFormVO vo) {
        return myService.run(process_id, className, vo);
    }

For more details about Path variable and Request parameter you can have a look at this tutorial.

EDIT: If you want to get these attributes from the request, then first parameter will be HttpServletRequest request in your controller method. Pass the request parameter to your service and there you can user request.getParameter("paramName") and request.getAttribute("attributeName") to access the values.

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79
  • this is not hat i want, the controller work fine i just forgot to copy it i will update my question, but i want later to get those information from HTTPRequest – satck djallil Feb 12 '19 at 10:42