Struts2 jackson throws exception for hibernate pojo - json

I am getting following exception on struts2 json...any ideas?
I am using Hibernate: 3.6.1, and struts2 with json plugin.
Exception: java.lang.NoSuchMethodException: com.model.Task.getHibernateLazyInitializer()

This is a pain... Hibernate/JPA decorates the entities so that they can do their magic. Long story short you need to prune the entity before returning it as json using exclude properties.
Easiest way is to examine the object at run time, you'll find the offending property then create an exclude regex to prevent the json plugin from serialization.
As a sanity test you can prove there is a bad property by simply defining include properties for the properties you know to exist, which will produce the object you need although it makes what should be an automatic process a pretty manual one, where fining the right exclude property aught to be the same between all hibernate entities.
For examples of include and exclude parameters see: http://struts.apache.org/2.2.3/docs/json-plugin.html

Related

How to define custom handling for a response class in Spring doc?

I've been using arrow-kt and spring together a lot lately. I've actually constructed a bridge between the two with several key features, one of which is a spring controller that returns an Either will automatically unwrap it and either handle the exception (Left) or return the result (Right). My long term goal is to publish this as a library.
My latest obstacle is Swagger, or more accurately springdoc openapi. Obviously it is seeing the Either, but i want it to show only the Right value as the success response type. While I know there are annotations where I can set the response model on each controller method individually, I'm trying to avoid this.
My real goal is to setup some global converter so that wherever Swagger sees an Either it will automatically unpack this. I'm just not super familiar with the customization API in Spring doc, and everything I Google just points me to the ApiResponse annotation solution.
How can I define default handling for this type of response?

How to apply low-level character filter to Jackson JSON parsing?

I have a (JSON) REST-based application based on Spring and RESTEasy. We have a requirement to limit the set of characters that are accepted as input to the services. I could apply the restriction to the POJOs themselves during or after deserialization, but it seems more efficient to insert some kind of filter into the Jackson parsing as it's reading the JSON stream, since it's obviously inspecting each character at some point anyway.
Question is, does Jackson provide a plug-in point to do that? Something that would enable me to decorate or override the low-level parsing at such a time that I could verify each character of each property value is valid (i.e., in a particular set according to the app requirements).
If you mean allowed characters within JSON String values, no, there is no filter or transformer functionality currently. You could consider implementing JsonParserDelegate, which could intercept calls to getText(), but that may not be very clean mechanism. Alternatively you could first read content as tree (JsonNode), traverse it and cleanse String values; and only after that do data-binding using ObjectMapper.convertValue(fromNode, MyType.class).
In the context of RESTEasy, you can inject the custom JsonParserDelegate by extending MappingJsonFactory and overriding the _createJsonParser(...) methods to return the custom delegate. Then implement an extension to JacksonJsonProvider (Jackson's JAX-RS provider) that creates an ObjectMapper with your custom factory in its constructor. Examples of this can be found in this answer to a related question.

Configure Play/Jerkson to deserialize single quoted (non-standard) JSON w/o duplicating a lot of code

In a Play 2.0 application, I need to deserialize some JSON from a source which I don't control which uses single-quotes around strings -- where the JSON spec calls for double-quotes.
The solution using Jackson is here:
Configure Jackson to deserialize single quoted (invalid) JSON
But trying to implement this solution in play2.0 I hit a wall of static objects and private classes... it should be enough to replace object JerksonJson with one implementing the solution linked above at initialization, but because it is a static object it can't be extended, and id I try to copy it into my code I need to drag along classes PlaySerializers, PlayDeserializers, JsValueDeserializer,... I stopped here, as it looked like too much.
Is there a clean solution?
How about trying to fix the invalid json string by replacing every ' in it with a "?
That would work if 's are only used for specifying strings.
I realize that this may not help too much with Play framework part, but perhaps you could use Jackson Scala Module instead of Jerkson? Doing that should make it easier to just use ObjectMapper, with Scala module registered, instead of having to use Jerkson-specific handlers.

How to have Jackson deserialize a cyclical graph that has been run through JSOG.stringify(myCyclicalGraph)

I am currently using the this Jackson plugin
Which serialized my cyclical graphs. Then on the client I use the JSOG to decode the {#ref} objects like this:
JSOG.decode(data)
The problem comes when I am trying to send the json back up to the server. If I don't do anything to the data I get a "Maximum call stack size exceeded", obviously because my js object is cyclical. I try using:
JSOG.stringify(data);
But then Jackson chokes on all the #id and #refs:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "#id"
Has any one figured out how to do this?
Both sides need to be able to support JSOG convention. It is not part of JSON specification, nor can it be automatically detected; so Jackson can not process it without help.

JAXB A cycle is detected in the object graph

I want to convert my pojo to json with JAXB, my pojo have one to many relation, and when i convert my pojo to json, JAXB generate error "A cycle is detected in the object graph. This will cause infinitely deep XML".
I read from web that, this problem can be solved with help from #XmlID and #XmlIDREF, but there is one problem, my Id attribute is not String type but Long. and as far as i know #XmlID can be used only with String property.
Other web suggest using eclipselink MOXy, but MOXy cannot generate json.
As you mentioned in your question EclipseLink MOXy (I'm the tech lead) has the #XmlInverseReference annotation to solve the problem of bidirectional relationships. As of EclipseLink 2.4 MOXy can produce/consume JSON.
For More Information
http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
You have a cyclic reference problem in your definition.
Try putting #XmlTransient above the problematic definition.
Also, about XmlID and string type, see http://markmail.org/message/up6vrzjixxrvy5th.
The JAXB specification requires that the property marked with #XmlID be a String property.
MOXy impl allows to use long.
One hack to keep using full JAXB compliant implementation would be to duplicate your id in a String field (before serialising)
Don't know so much about JAXB but XStream makes you able to use different modes and some of these modes will give references to the xpath address (absolute or relative) of an element in your xml, if these elements are already displayed.
(And you can do Json with XStream)
I faced similar problem when I wanted to convert my POJO to JSON with JaxRS. The MoxyJsonProvider is the default option of eclipselink but it fails to parse the JSOG (where cycles exists in the JSON structure). Jackson Jaxb Provider does this better with ObjectMapper.
I have elaborated in this answer below, about how to invoke Jackson Provider instead of Moxy. You will need jackson packages in your pom xml.
https://stackoverflow.com/a/60319306/5076414