0

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: 2017年6月04日 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.

asked Jun 4, 2017 at 21:25

2 Answers 2

2

This worked:

I had two issues in my spring-boot java method,

  1. I'm specifying @RrequestParam for the arguments, where I should be using @RequestBody instead
  2. I have multiple of such method params where in POST it should be a single (see footnote) @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

answered Jun 6, 2017 at 16:21
Sign up to request clarification or add additional context in comments.

Comments

0

Refer below sample,

curl -H "Content-Type: application/json" --data @body.json 
http://localhost:8080/ui/webapp/conf
answered Jun 5, 2017 at 8:57

5 Comments

thanks for the suggestion but I still get the same behaviour as before
Can you try with chrome postman extension? If you are able to get response using any post client such as postman then curl should also work as it is a client. PostMan : chrome.google.com/webstore/detail/postman/…
sorry for the delay, just tried chrome postman, still getting same behaviour, to make sure I'm not using anything funny in my spring boot app I cloned spring-boot-tutorial. Only added code related to dummyApi
unfortunately no :(

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.