0

Wondered if someone could help me out. I have a JSON request

{
    "blue": "blue",
    "red": "red",
    "greens" : {
        "lightGreen": "lightGreen",
        "darkGreen": "darkGreen"
    }
}

which I want to map to a pojo using @RequestBody

done like this:

@PostMapping(path = "/colors", headers = "Accept=application/json")
public void generateClaimDocument(@Valid @RequestBody Colors colors) {
        if (colors != null) {
            service.doSomethingWithColors(colors);
        }
    }

Which works fine for blue and red, but it is not mapping JSON greens object at all. Never done this before so could someone shed some light onto how I can do this?

PoJo's:

public class Colors {

    private String blue;
    private String red;
    private Greens greens;
    //getters and setters
}

public class Greens {

    private String lightGreen;
    private String darkGreen;
    //getters and setters
}
2
  • A comma seems to be missing after "red": "red" . Commented Jul 18, 2018 at 10:05
  • @Berger So there was, typo on my part when writing the question still wont fix my issue though :) Commented Jul 18, 2018 at 10:08

1 Answer 1

3

I couldn't find any issue using your code. Tried this example:

    @RestController
    public static class ColorsService {

        @PostMapping(path = "/colors", headers = "Accept=application/json")
        public void generateClaimDocument(@Valid @RequestBody Colors colors) {
            if (colors != null) {
                System.out.println(colors);
            }
        }
    }

    public static class Colors {

        private String blue;
        private String red;
        private Greens greens;
        //getters and setters

        public String getBlue() {
            return blue;
        }

        public void setBlue(String blue) {
            this.blue = blue;
        }

        public String getRed() {
            return red;
        }

        public void setRed(String red) {
            this.red = red;
        }

        public Greens getGreens() {
            return greens;
        }

        public void setGreens(Greens greens) {
            this.greens = greens;
        }

        @Override
        public String toString() {
            return "Colors{" + "blue=" + blue + ", red=" + red + ", greens=" + greens + '}';
        }
    }

    public static class Greens {

        private String lightGreen;
        private String darkGreen;
        //getters and setters

        public String getLightGreen() {
            return lightGreen;
        }

        public void setLightGreen(String lightGreen) {
            this.lightGreen = lightGreen;
        }

        public String getDarkGreen() {
            return darkGreen;
        }

        public void setDarkGreen(String darkGreen) {
            this.darkGreen = darkGreen;
        }

        @Override
        public String toString() {
            return "Greens{" + "lightGreen=" + lightGreen + ", darkGreen=" + darkGreen + '}';
        }
    }

which prints exaclty what you posted:

Colors{blue=blue, red=red, greens=Greens{lightGreen=lightGreen, darkGreen=darkGreen}}

Sign up to request clarification or add additional context in comments.

2 Comments

Above code is perfectly correct in terms of logic... there might be problem with POJO class getter and setter.... JSON object is correct...
Yup, it is... Somehow fixed it myself... Not even sure what I did to break it. Thanks

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.