Can gson convert Spring Data interface projections? - spring

When I use gson and its HttpConverter in Spring Module,it seems to throw an exception like:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Attempted to serialize java.lang.Class: com.manager.projections.StaffProjection. Forgot to register a type adapter?; nested exception is java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: com.manager.projections.StaffProjection. Forgot to register a type adapter?
and I'm confused.Can gson convert Spring Data interface projections?

Related

Facing issues while using spring cassandra - UDT Type, not able to map udt type to udtvalue

Could not inline literal of type AddressUDT. This happens because the driver doesn't know how to map it to a CQL type. Try passing a TypeCodec or CodecRegistry to literal().; nested exception is java.lang.IllegalArgumentException: Could not inline literal of type AddressUDT. This happens because the driver doesn't know how to map it to a CQL type. Try passing a TypeCodec or CodecRegistry to literal().",
You need to define a custom converter so Cassandra knows how to map your UDT to a CQL type.
You need to create a class that extends AbstractCassandraConfiguration to do custom conversions. Have a look at the Spring Data documentation on Custom Converters for details and examples. Cheers!

How to access Couchbase RAW document with spring-boot-couchbase CrudRepository

I am migrating an old application from using the Couchbase java client, 2.2.5 to Spring-boot-couchbase. I would like to use Spring Data's Crud Repository.
The problem I am running into is that the document in Couchbase is RAW format, (See more info here: Couchbase non-json docs). I have set up a crud repository interface and when connecting to other buckets with other json-formatted data, it works perfectly. When attempting to read the byte array data I get this error:
org.springframework.dao.DataRetrievalFailureException: Flags (0x802) indicate non-JSON document for id 9900fb3d-1edf-4428-b9e6-0ef6c3251c08, could not decode.; nested exception is com.couchbase.client.java.error.TranscodingException: Flags (0x802) indicate non-JSON document for id 9900fb3d-1edf-4428-b9e6-0ef6c3251c08, could not decode.
I have tried the following object types in the repository:
public interface MyRepository extends CouchbasePagingAndSortingRepository<Object, String> {
Object
byte[]
The object with an id and byte[] fields (and setters & getters)
Objects from the java-client
CouchbaseDocument
RawJsonDocument
AbstractDocument
I've also attempted to write a custom jackson mapper but the error message stays consistent in that it's trying to deserialize a non-json document.

Error while Unmarshalling XML using JAXB/Spring OXM

I am trying to convert xsd to Java objects using xjc. classes are generated.
But when i try to unmarshall an sample xml file to java object using spring OXM, it fails with the below error.
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:validfilenotification", local:"ValidFileNotification"). Expected elements are <{urn:notificationbase}ActionRequestedGrp>,<{urn:notificationbase}DocTypeIndicCd>,<{urn:notificationbase}FATCANotificationCd>,<{urn:notificationbase}FATCANotificationHeaderGrp>,<{urn:notificationbase}FATCAReportTypeCd>,<{urn:notificationbase}FieldErrorGrp>,<{urn:notificationbase}FileTypeCd>,<{urn:notificationbase}MessageTypeCd>,<{urn:notificationbase}OriginalFileMessageSpecGrp>,<{urn:notificationbase}OriginalFileMetadataGrp>,<{urn:notificationbase}OriginalFileProcessingDataGrp>,<{urn:notificationbase}OriginalPaperRecordMetadataGrp>,<{urn:notificationbase}PaperRecordDocumentTypeCd>,<{urn:notificationbase}RecordLevelErrorCd>,<{urn:notification
can somebody point what am i doing wrong.

Use JSON deserializer for Batch job execution context

I'm trying to get a list of job executions which have been stored in Spring batch related tables in the database using:
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
The above method call seems to invoke ExecutionContextRowMapper.mapRow method in JdbcExecutionContextDao class.
The ExecutionContextRowMapper uses com.thoughtworks.xstream.Xstream.fromXML method to deserialize the JSON string of JobExecutionContext stored in DB.
It looks like an incorrect or default xml deserializer is used for unmarshalling JSONified JobExecutionContext.
Is there any configuration to use a JSON deserializer in this scenario.
The serializer/deserializer for the ExecutionContext is configurable in 2.2.x. We use the ExecutionContextSerializer interface (providing two implementations, one using java serialization and one using the XStream impl you mention). To configure your own serializer, you'll need to implement the org.springframework.batch.core.repository.ExecutionContextSerializer and inject it into the JobRepositoryFactoryBean (so that the contexts are serialized/deserialized correctly) and the JobExplorerFactoryBean (to reserialize the previously saved contexts).
It is important to note that changing the serialization method will prevent Spring Batch from deserializing previously saved ExecutionContexts.

Parsing a POJO to JSON using JAX-RS and not using any speific implemnation code

I was looking at internals of jersey, on how it converts a simple POJO to Json. Jersey has an interface Providers, which will provide list of contextResolvers given the class and mediaType. once we get the contextResolver, we can get the Context and Marshaller from it, which can used to get the json string, like show below.
lets assume we want to serialize the Pojo "obj". the code will look like follows
Providers ps = ...
ContextResolver<JAXBContext> resolver = ps.getContextResolver(obj.getClass(), MediaType.APPLICATION_JSON_TYPE);
JAXBContext ctx = resolver.getContext(obj.getClass());
ctx.createMarshaller().marshal(obj, writer);
By above way we can convery any POJO which has a valid context resolver to json. but the question is how do we get the handler for Providers.
PS: i have not compiled this code, but from what i can see from source this is what jersey does. On why i am doing all this stuff, so that we can convert a object to JSON directly with JAX-RS apis. instead of using any implementation code.
In a JAX-RS resource clas, use the #Context annotation to tell Jersey to inject the producers:
#Context Producers producers;
So your question is : "how to find providers" ?
You have to write code to find classes in the classpath which are Annotated with #Provider.

Resources