3

Using Spring Boot, I want to build a Rest controller. But Spring fails to convert Json to Java object automatically. And yes:

  • I have empty constructors
  • I tried @RequestParam, @RequestBody, @RequestPart
  • My JSON parameter is valid (I tried to parse with Gson, and it worked)

Here is my REST Controller:

@RestController
public class HelloController {

     @RequestMapping(value= "/abc", method=RequestMethod.POST)
     @ResponseStatus(HttpStatus.OK)
     public @ResponseBody MyResponse handlePurchase(@RequestParam A request){
         return new MyResponse("Simply the best");
     }

}

Here is A.java:

public class A implements Serializable {

    private B something;

    public B getSomething() {
        return Something;
    }

    public void setSomething(B something) {
        this.Something = something;
    }

    @Override
    public String toString() {
        return "A [Something=" + something + "]";
    }

    public A(B something) {
        super();
        this.Something = something;
    }

    public A() {
        super();
    }


}

Here is B.java which is used by A.java:

public class B implements Serializable {
    private String something;
    private int catsNumber;
    private int dogsNumber;

    public String getSomething() {
        return something;
    }

    public void setSomething(String something) {
        this.something = something;
    }

    public int getCatsNumber() {
        return catsNumber;
    }

    public void setCatsNumber(int catsNumber) {
        this.catsNumber = catsNumber;
    }

    public int getDogsNumber() {
        return dogsNumber;
    }

    public void setDogsNumber(int dogsNumber) {
        this.dogsNumber = dogsNumber;
    }

    @Override
    public String toString() {
        return "B [something=" + something + ", catsNumber=" + catsNumber + ", dogsNumber=" + dogsNumber + "]";
    }

    public B() {
        super();
    }


}

Hope someone can help me on this, neither worked yet:

  • Spring.io
  • other Stackoverflow Q&As
6
  • Do you have any exception ? Commented Aug 12, 2016 at 8:58
  • Can you share your message body json? Commented Aug 12, 2016 at 9:23
  • can you post your pom.xml Commented Aug 12, 2016 at 10:25
  • @kuhajeyan: posted Commented Aug 12, 2016 at 12:36
  • can you change to @RequestMapping(value= "/abc", method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes= MediaType.APPLICATION_JSON_VALUE) and instead of \@RequestParam use \@RequestBody Commented Aug 12, 2016 at 13:09

1 Answer 1

2

@RequestParam indicates an url parameter http://foo.com?parameter=x. When using post, you need to define a @RequestBody

@RequestMapping(value= "/abc", method=RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody MyResponse handlePurchase(@RequestBody A a){
     return new MyResponse("Simply the best");
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but still not working at all (Before changing the "@RequestParam" to "@RequestBody", posting worked well for me): Here's what I get for posting (www-form-urlencoded) { "timestamp": 1470992850627, "status": 415, "error": "Unsupported Media Type", "exception": "org.springframework.web.HttpMediaTypeNotSupportedException", "message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", "path": "/purchase" }
And the simple form-data:"org.springframework.http.converter.HttpMessageNotReadableException", "message": "Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: java.io.PushbackInputStream@478e62db; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: java.io.PushbackInputStream@478e62db; line: 1, column: 3] ... }
What tool are you using to generate you post? Can you share your application (github/bitbucket/...)
@Janekx - Set the Content-Type to application/json while posting your request
Sorry, I've forgotten, but that is also set.
|

Your Answer

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.