2

I'm trying to figure out how to bind nested JSON data received from an ajax post to a POJO that has a one to many relationship with another POJO in the spring mvc framework.

for example I have 2 classes:

public class MagTemplate implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private BigInteger magTemplateId;

    private String templateName;

    //bi-directional many-to-one association to MagZone
    @OneToMany(mappedBy="magTemplate", cascade = CascadeType.PERSIST)
    private List<MagZone> magZones;

    public MagTemplate() {
    }

    //getters and setters
}

public class MagZone implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private BigInteger magZoneId;

    private String zoneName;

    //bi-directional many-to-one association to MagTemplate
    @ManyToOne
    @JoinColumn(name="magTemplateId")
    private MagTemplate magTemplate;

    public MagZone() {
    }

    //getters and setters
}

And I have a JSON object that looks like this:

{
    templateName : "template 1",
    magZones : [

            zoneName : "zone 1"
        },
        {
            zoneName : "zone 2"
        },

            zoneName : "zone 3"
        }
    ]
}

When I tried to create a controller that looked like this:

@RequestMapping(value = "/saveTemplate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = "application/json")
@ResponseBody
public AjaxResponse saveTemplate(HttpServletRequest request, @RequestBody MagTemplate magTemplate){
    //controller code    
}

and then do an ajax post like so:

$.ajax({
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    contentType : "application/json",
    method: "POST",
    dataType: 'json',
    url: url,
    data: JSON.stringify(jsonData),
    success: function(response){

    },
    error:function(jqXHR, textStatus, errorThrown){
        console.error(jqXHR);
        console.error('status: ' + textStatus + "\n" + "error: " + errorThrown);
    }
 });

I get an HTTP400 error saying that its a bad request. I have the spring jackson-databind dependency and it seems to be working because I can bind a single MagZone object with another controller.

How do I get the spring controller to do a deep bind of the JSON data so that I have a single MagTemplate object with a list of the 3 MagZone objects? I know that this is possible in other mvc frameworks but I cant seem to find an example of it working in spring.

David
  • 377
  • 5
  • 14
  • https://stackoverflow.com/q/43487344/6756805 you can refer this for modelling your pojo.. I had same issue and fixed it – Hema Sep 27 '17 at 06:32
  • The difference is I'm not pulling the JSON in a a string. In my method declaration the parameter is the same type as my POJO and jackson-databind is supposed to handle the mapping. If I just send a single child it will map just find to the child class but if I try to send the parent containing the child it breaks down. – David Sep 27 '17 at 16:50

1 Answers1

1

Add @JsonIgnore to the fields (or methods) which you don't use in JSON to exclude from deserialization.

Alex
  • 11,451
  • 6
  • 37
  • 52
  • The JSON object I am passing has all the same fields as the POJO and the only methods in the java class are just the getters and setters – David Sep 27 '17 at 04:36
  • 1
    If so, try to add `@JsonIgnore` to the field with `@ManyToOne`. It might be a circular reference because of it. – Alex Sep 27 '17 at 04:45