Reputation: 445
Have looked at other related posts but nothing seemed to work.
Basically I'm trying to figure out how to pass data to my restful spring-boot app.
Here is the curl command I'm using:
$ curl -iX POST -H 'Content-Type: application/json' -d @args.json http://localhost:8080/myapp/dummyApi
With args.json
contains:
file: args.json:
{
"arg1": "hello",
"arg2": 10,
"arg3": {
"a": "1",
"b": "2"
}
}
The api is defined in MyController.java
as such:
file: MyController.java
@RestController
@RequestMapping("/myapp")
public class MyController {
//...
static class DummyRet {
private String foo;
public DummyRet(String f) {
foo = f;
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
@RequestMapping(value="/dummyApi", method=RequestMethod.POST)
public DummyRet dummyApi(@RequestParam(value = "arg1", required = false, defaultValue = "") String arg1,
@RequestParam(value = "arg2", required = false, defaultValue = "") Long arg2,
@RequestParam(value = "arg3", required = false) Map<String, String> arg3) {
LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
LOGGER.info("Arguments received:");
LOGGER.info("arg1: " + arg1);
LOGGER.info("arg2: " + arg2);
LOGGER.info("arg3: " + arg3);
LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
return new DummyRet("foo");
}
//...
}
The return of the curl command is 200 success (since non of the arguments is required) but the values of args are not reaching over to dummyApi
method
$ curl -iX POST -H 'Content-Type: application/json' -d @args.json http://localhost:8080/myapp/dummyApi
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 04 Jun 2017 15:37:42 GMT
{"foo":"foo"}
The server console looks like this:
2017-06-04 18:17:56.818 DEBUG 32258 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/myapp/dummyApi]
2017-06-04 18:17:56.818 DEBUG 32258 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /myapp/dummyApi
2017-06-04 18:17:56.818 DEBUG 32258 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public com.xx.controllers.MyController$DummyRet com.xx.controllers.MyController.dummyApi(java.lang.String,java.lang.Long,java.util.Map<java.lang.String, java.lang.String>)]
2017-06-04 18:17:56.819 INFO 32258 --- [nio-8080-exec-7] c.p.controllers.MyController : >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2017-06-04 18:17:56.819 INFO 32258 --- [nio-8080-exec-7] c.p.controllers.MyController : Arguments received:
2017-06-04 18:17:56.819 INFO 32258 --- [nio-8080-exec-7] c.p.controllers.MyController : arg1:
2017-06-04 18:17:56.819 INFO 32258 --- [nio-8080-exec-7] c.p.controllers.MyController : arg2: null
2017-06-04 18:17:56.819 INFO 32258 --- [nio-8080-exec-7] c.p.controllers.MyController : arg3: null
2017-06-04 18:17:56.819 INFO 32258 --- [nio-8080-exec-7] c.p.controllers.MyController : >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2017-06-04 18:17:56.819 DEBUG 32258 --- [nio-8080-exec-7] m.m.a.RequestResponseBodyMethodProcessor : Written [com.xx.controllers.MyController$DummyRet@c35d46f] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@301ec38b]
2017-06-04 18:17:56.819 DEBUG 32258 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-06-04 18:17:56.819 DEBUG 32258 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Successfully completed request
I hope I haven't left out any of the important details, but please let me know if anything is missing.
Upvotes: 0
Views: 4148
Reputation: 445
This worked:
I had two issues in my spring-boot java method,
@RrequestParam
for the arguments, where I should be using @RequestBody
instead@RequestBody
But since I need to pass multiple arguments over to the method the solution was to wrap these arguments in a backing object, as such:
public static class Args {
// members
private String arg1;
private Long arg2;
private Map<String, String> arg3;
// accessors
public String getArg1() {return arg1;}
public void setArg1(String arg1) {this.arg1 = arg1;}
public Long getArg2() {return arg2;}
public void setArg2(Long arg2) {this.arg2 = arg2;}
public Map<String, String> getArg3() {return arg3;}
public void setArg3(Map<String, String> arg3) {this.arg3 = arg3;}
}
The receiver method would then be:
@RequestMapping(value="/dummyApi", method=RequestMethod.POST)
public DummyRet dummyApi(@RequestBody Args args) {
LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
LOGGER.info("Arguments received:");
LOGGER.info("arg1: " + args.getArg1().toString());
LOGGER.info("arg2: " + args.getArg2().toString());
LOGGER.info("arg3: " + args.getArg3().toString());
LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
return new DummyRet("foo");
}
Footnote: In fact it is possible to pass multiple params through custom implementation as described in this SO response. Haven't tried it
Upvotes: 2
Reputation: 2445
Refer below sample,
curl -H "Content-Type: application/json" --data @body.json
http://localhost:8080/ui/webapp/conf
Upvotes: 0