On Fri, Dec 14, 2018 at 4:06 AM Burak Emre Kabakcı <[email protected]> wrote: > > We use Jackson for parsing JSON data in our RESTFul API, our models also > automatically validate the data so we mark properties as required, use enums > wherever possible etc. For example; if a required property is not set, > Jackson throws an exception: > > Missing external type id property 'type'↵ at [Source: UNKNOWN; line: -1, > column: -1] (through reference chain: > io.rakam...SqlReportOptions["variables"]->java.util.ArrayList[0]) > > For the data: > {"variables": [{"name": "test"}]} > > We enforce the `type` property of each variable to be set so the error > message makes sense. However; we want to show the following error message to > the user: > > variables[0].type is required. > > We may catch the JacksonException and generate the error message but it looks > like it's not that easy. In JsonMappingException, there is path object but we > could generate the json path `variables[0].type` since it uses class names > instead of attribute names. Which path should we follow in this case? >
I think that since exception has path information, it has to be a `JsonMappingException`, and method you need is `getPath()`. That gives you logical path via properties and indexes. Alternatively `JsonProcessingException` (subtype of JsonMappingException) has method `getProcessor()` which may return underlying `JsonParser` (unfortunately this depends on who threw exception, so not guaranteed to be set at all); and if so, `JsonParser.getParsingContext()` gives access to input location, as well as nesting information (i.e. all parent properties from json input). -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
