0

java newb here. Probably this has been covered somewhere before, but I just can't seem to find it. RESTish service on Java 8 + Spring 4 + Jackson 2. What I want to do, is unmarshal JSON into an object. It works just fine with minimal setup:

public class A {
    private int id;
    private double val;

    getters/setters
}

Then I have my restcontroller:

@RestController
@RequestMapping(value = "/item/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public class MyController {
    public A test(@Valid @RequestBody A a) {
        return a;
    }
}

If I post valid JSON

{"id": 1, "val":5.55}

all is dandy. If I post

{"id": cat, "val":5.55}

by default I get response of 400. I can define @ExceptionHandler to deal with HttpMessageNotReadableException, so it would return some sort of descriptive JSON back, pointing out the mistake.

Problem is, that if I get more than one mistake in JSON, such as: {"id": cat, "val":"dog"} unmarshaller will throw exception on first node and stop parsing, requiring the client to fix issues one-by-one and reposting them, just to get next error response. It might not be an issue in this particular case, but it becomes very uncanny when dealing with larger datasets. So I would like to somehow convince lord Jackson to parse whole thing before throwing exception. Suggestions?

stiebrs
  • 379
  • 3
  • 13

1 Answers1

-1

You can use custom deserializer

Or you can validate incoming JSON data inside a REST service!! As explained here

Community
  • 1
  • 1
Sushantkumar M
  • 337
  • 2
  • 7