I'm using Spring Batch and Spring Boot and am having a problem when deserializing UUIDs as Jackson reports they are not a trusted class. According to the Spring docs I can add it to the list of trusted classes at construction time:
"Implementation that uses Jackson2 to provide (de)serialization. By default, this implementation trusts a limited set of classes to be deserialized from the execution context. If a class is not trusted by default and is safe to deserialize, you can add it to the base set of trusted classes at construction time or provide an explicit mapping using Jackson annotations"
However there are no examples that I can find. I have tried the following but it doesn't make any difference:
#Bean
ExecutionContextSerializer executionContextSerializer() {
return new Jackson2ExecutionContextStringSerializer("java.util.UUID");
}
Any idea how to do it?
EDIT
I tried Luca Liechti's solution in this post
Spring Batch not deserialising dates
which allowed the deserialization of UUIDs however it caused another issue with converting a List to an ArrayList
Related
I have this query
http://localhost:8555/list/csv?search={}
Where search is a json object (omitted other params as they are irrelevant here).
How can i convert this into a nested object?
public record CsvParams<T>(
T search,
/* Other query params */ ) {}
Right now im getting error that string cannot be cast into object.
class java.lang.String cannot be cast to class classname
Is there anyway to do this? Old solution uses ObjectMapper to convert string into corresbonding object. I was hoping that maybe there is a way to do it more simpli and remove this boilerplate.
Any single value of a query param can't be automatically converted to a non-primitive type. You can convert multiple params to a class, but not one that happens to be a JSON AFAIK. But you can create a converter custom deserialiser and then use it in different controllers, but in the end you'd still use an ObjectMapper.
More info on how to do the latter here: https://www.baeldung.com/spring-mvc-send-json-parameters
If you have to work with query params than I don't think you can have it converted automatically by Spring boot. But if you work with POST or PUT methods and can pass your params as request params in request body your JSON params can be automatically converted to class instances by Spring boot and no effort required from you. However, if you have to work with query param (say you have to use method GET so you have no request body) than you can use Json-Jackson library or Gson library to parse your Json into class instance. If you use Jackson you will need to use class ObjectMapper. For Jackson lib info see this site, for ObjectMapper class see Javadoc here. However, I wrote my own JsonUtils that is very good for simple usecases like yours. It allows to to parse simple JSON into a class with a single method. It is very simple and strait forward. It is a thin wrapper over Jackson library. See the Javadoc for method readObjectFromJsonString. Class JsonUtils is part of Open Source MgntUtils library. You can get it as Maven artifact on Maven Central and as a jar (with source code and Javadoc) on Github
For example there are two classes with same name Group in different packages.
But when referring to models in swagger ui only one model is being shown and even the response mapping is not proper, swagger is incorrectly referring to these models.
Yes. Adding the below property solves this issue.
springdoc.use-fqn=true
But after adding this, the schemas in swagger ui start showing up with the complete package name. To avoid this try annotating your model classes with:
#Schema(name = "class name to be displayed")
I meet the same problem today with springdoc-openapi-ui:1.5.9, and maybe solved it.
Simply add key-value pairs to application.properties.
springdoc.use-fqn=true
Then org.springdoc.core.SpringDocConfigProperties read this value, and pass it to io.swagger.v3.core.jackson.TypeNameResolver.
I'm using Joda objects (DateTime and DateTimeZone) in a document and whenever I access it via the REST interface I get entries with fields like this
lastAggregationDate: { content: "2016-07-12T17:58:43.643Z" }
instead of
lastAggregationDate: "2016-07-12T17:58:43.643Z"
I have the Joda Jackson dependencies declared and I see the de/serializers for these types so I'm puzzled as to what's at work here.
I've duplicated this behavior in a slightly modified Spring sample project but using Java's native date types rather than Joda's. I've added a date of birth property to the Person object and modified the shouldRetrieveEntity test to look for $.dateOfBirth.content. I've confirmed the serializer is being used and it seems like the LocalDate object is being treated as a resource rather than as a simple property.
This is fixed in Spring Data Hopper-SR4:
https://jira.spring.io/browse/DATAMONGO-1498
The issue results from Spring Boot not setting up MongoMappingContext correctly. A ticket has been created for Spring Boot and the fix is anticipated for the 1.4.1 release (credit for this answer goes to Oyku Gencay and Oliver Gierke). For more detail, see the ticket or the pull request.
I'm trying to use DynamicEntity to unmarshal some simple JSON, and it's totally bombing on me. Docs are rather sparse, is it even possible to do this? I'm basically doing this;
JAXBContext jaxbContext = JAXBContext.newInstance(DynamicEntity.class);
DynamicEntity entity = (DynamicEntity) jaxbContext.createUnmarshaller().unmarshal(entityStream);
This is straight from the XML docs here: https://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/XmlToDynamicEntity
And I get;
Caused by:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
org.eclipse.persistence.dynamic.DynamicEntity is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at org.eclipse.persistence.dynamic.DynamicEntity
Has anyone managed to get this to work? I'm basically trying to avoid building POJOs since my backend store doesnt care about them anyway, I just want to deserialize to a generic object and pass it along. In .NET I'd just use dynamic but I'm pretty stumped on how to do this with Moxy.
In order to get DynamicEntity, it is neccessary to use DynamicJAXBContext. It can be configured using the following in jaxb.properties file:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory
or you can use DynamicJAXBContextFactory directly.
Although Dynamic MOXY does not require any java classes for the bindings (the POJOs), you need to provide binding metadata when creating the context. It can be XSD or binding.xml that describes the mapping. So instead of creating the context using JAXBContext.newInstance(DynamicEntity.class), you would need to use alternative methods - see links below.
See example here (using XSD):
https://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/JAXBContextFromXMLSchema
or more complex example using binding.xml and also JSON:
https://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic/Flickr
I am planning to provide a interface to iOS Apps, the developer asked to add an additional field call class contains the name of the pojo I used on server so he can convert to his class on client easily. The problem is I have to do this when the json lib is processing the values.I think the registerJsonValueProcessor can do the trick. I got then class name by obj.getClass().getName() but I still have no idea how to attach it to json-lib.
If you are using jackson for serialization/deserialization of json than please apply the annotation JsonTypeInfo on your class. It could help you
#JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")