简体   繁体   中英

Spring REST Controller mapping JSON

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
}

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}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM