On Wed, May 22, 2019 at 4:25 AM Uzh Valin <[email protected]> wrote: > > Correct, when I construct this Map<String, Object> and pass it back as part > of exception handling Rest response body (500 HTTP status), I see each item > has a proper value. > > However, when the exception is thrown on the caller side, and I try to > examine the response body > (HttpStatusCodeException.getResponseBodyAsString()), what I see is a JSON in > the following format: > > { > "firstProperty": { > "NodeType":"OBJECT", > "POJO":false, > ... bunch of other properties > } > ... repeat for other properties > } > > Which I am not able to read into an object type (MainObject), and frankly, I > don't see any relevant POJO/bean properties when the Map is constructed. > > If I create a custom model and simply add that as part of object to Response, > the serialization would yield a proper result on the caller side, and I am > able to readValue() and map it to a proper type.
Jackson would never serialize `JsonNode` as POJO, which is what output above looks like. Is it possible you might have something else configured to handle JSON serialization? -+ Tatu +- > On Wednesday, May 22, 2019 at 1:10:21 AM UTC-4, Tatu Saloranta wrote: >> >> On Tue, May 21, 2019 at 7:23 PM Uzh Valin <[email protected]> wrote: >> > >> > Thanks Tatu, but how do I read it back when I send this as part exception >> > JSON body? It seems like when if read the body with >> > HttpStatusCodeException.getResponseBodyAsString(), it contains all sort of >> > JsonNode data rather than the actual data that I send back from the Rest >> > producer. >> >> I am not sure I understand -- `JsonNode` is simply node-based >> representation of JSON, which serializes to textual content like >> POJOs. There is nothing special about it, it's just one way to >> manipulate JSON content that may or may not have specific structure. >> The other side has no idea what kind of in-memory representation >> server uses: it's just json. >> >> -+ Tatu +- >> >> > >> > >> > On Tuesday, May 21, 2019 at 7:08:22 PM UTC-4, Tatu Saloranta wrote: >> >> >> >> On Tue, May 21, 2019 at 3:57 PM Uzh Valin <[email protected]> wrote: >> >> > >> >> > Ok, but how does Jackson know which custom mapper to call if we are >> >> > simply putting obj.getFirstProperty() in the response map? I know need >> >> > one value and would want the serializer to ignore all other properties >> >> > regardless whether they have been initialized with default values. >> >> >> >> It doesn't, but what I am saying is that your approach will not work as >> >> shown. >> >> If you add String values, they will be JSON Strings and must be >> >> escaped. That's how Strings are handled -- otherwise Jackson would >> >> have to somehow re-parse String into JSON, and then go back to >> >> serializing contents, adding significant overhead that is not usually >> >> needed. Assuming that String was valid JSON; if not it would either >> >> have throw exception, or quietly determine it has to be used as-is... >> >> or something. >> >> >> >> If you want to apply different rules there are a few ways you could >> >> achieve that -- custom serializers are one way -- or you could >> >> serialize-as-String-then-deserialize if you want to apply filtering. >> >> Perhaps latter is the way to go. >> >> >> >> In fact, you could probably use something like: >> >> >> >> JsonNode node = mapper.valueToTree(inputValue); >> >> results.put(key, node); >> >> >> >> which would convert from POJO into JsonNode -- and this does use >> >> serialize() methods, filtering, but with less overhead -- and then add >> >> JsonNode as value to be serialized by "parent" mapper. >> >> >> >> I think this might achieve what you are attempting here? >> >> >> >> -+ Tatu +- >> >> >> >> > >> >> > >> >> > On Tuesday, May 21, 2019 at 2:40:27 PM UTC-4, Tatu Saloranta wrote: >> >> >> >> >> >> Just one question: >> >> >> >> >> >> >> >> >>> >> >> >>> responseBody.put("firstProperty", >> >> >>> serializeFirstProperty(obj.getFirstProperty())); >> >> >>> responseBody.put("secondProperty", >> >> >>> serializeSecondProperty(obj.getSecondProperty())); >> >> >>> responseBody.put("thirdProperty", >> >> >>> serializeThirdProperty(obj.getThirdProperty())); >> >> >> >> >> >> >> >> >> Why do you serialize values? That is where "double-escaping" comes: >> >> >> you are adding JSON String within content to be JSON serialized. Just >> >> >> add values as is >> >> >> >> >> >> responseBody.put("firstProperty", obj.getFirstProperty()); >> >> >> >> >> >> and contents would get serialized just once. >> >> >> >> >> >> -+ 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]. >> >> > To view this discussion on the web visit >> >> > https://groups.google.com/d/msgid/jackson-user/e9420cec-0ddb-4af4-bfc7-1383bede4920%40googlegroups.com. >> >> > For more options, visit https://groups.google.com/d/optout. >> > >> > -- >> > 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]. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/jackson-user/ddd137c4-0f74-4976-9a86-64a9f7f1daba%40googlegroups.com. >> > For more options, visit https://groups.google.com/d/optout. > > -- > 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]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jackson-user/cac2aa77-01ce-478f-bc9b-7ba6d9553bd3%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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]. To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAL4a10g_RLySNOwE8YiLWfapB0kGoTz%3DhCDwE%2BR4sgcLnG5Fmg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
