Configure SpringBoot PropertyNamingStrategy only for deserialization in Jackson? - json

I wonder if it's possible to configure Jackson in Spring Boot to use different
PropertyNamingStrategy only for deserialization?
I know that it's possible to configure a global setting in properties e.g.:
spring.jackson.property-naming-strategy=SNAKE_CASE
Is it possible to do it only when deserializing?

Related

How to configure Jackson for HTTP REST response body in Spring

I am trying to build a REST service using Spring 4.3.4 and Spring Boot 1.4.2. What I need to do is to configure the Jackson instance in such a way that I can access the actual configuration of the object mapper (e.g. use fields only, don't use getter/setter methods etc).
After a long session of debugging Spring code, I tried all of the following:
#Bean for Jackson2ObjectMapper
#Bean for Jackson2ObjectMapperBuilder
#Bean for Jackson2ObjectMapperFactoryBean
None of the above had any effect. Ultimately, I think I've figured out the root cause of it all. Here's how Spring really creates it's Jackson instances:
AllEncompassingFormHttpMessageConverter calls new MappingJackson2HttpMessageConverter()...
... which builds a new Jackson2ObjectMapperBuilder...
...via static method Jackson2ObjectMapperBuilder.json()
ALL of the above is static (!!) and completely neglects any kind of configuration, regardless whether it's XML, Java #Configuration classes or even application.properties.
The question is: do I need to implement my own converter that replaces the AllEncompassingFormHttpMessageConverter (is that even possible?), or is there any other solution to configure the Jackson instance?

Scala JSON serialization support in Jersey 2.5

I have created a Jersey 2.5 Scala REST API Project.
I have a ResourceConfig file, we will call it MyApplication, that looks similar to this:
class MyApplication extends ResourceConfig {
packages(classOf[MyResource].getPackage().getName())
}
All it does is register the resource: MyResource. How can I configure Jersey (2.5) to provide out-of-the-box style JSON Serialization/Deserialization.
For example, here is what MyResource might look like:
#Path("/")
class MyResource {
#POST
#Produces(Array("application/json"))
#Consumes(Array("application/json"))
def getIt(request:SomeRequestModel) = {
/* Do something with the request, return some response model */
return new SomeResponseModel
}
}
So to reiterate, how can I configure Jersey to automatically deserialize and serialize the request and response models, respectively?
It's not actually Jersey that provides the serialisation, it simply draws on an implementation of JAX-RS to perform that role.
Assuming Jersey is a strict requirement, the easiest solution here is to use jackson with Scala bindings. You can find an example here: https://bitbucket.org/jordipradel/jersey-scala-example
If you're not completely tied to Jersey... Might I suggest trying either Spray or spray2-mini instead for a far more idiomatic Scala solution?
I have used Jersey little when developing Java REST services. However, I would say you appear to be conflating two concepts--registering JAX-RS providers and configuring providers to serialize/deserialize JSON.
To register a provider, you use ResourceConfig as you have done.
As for the second issue of configuring Jersey to "know" how to serialize/deserialize JSON:
"As stated in Section 4.3, “Auto-Discoverable Features” JSON-Processing media module is one of the modules where you don't need to explicitly register it's Features (JsonProcessingFeature) in your client/server Configurable as this feature is automatically discovered and registered when you add jersey-media-json-processing module to your classpath."
To add the Jackson flavor of that module to the classpath, you just manually put it there or do this with Maven:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5</version>
</dependency>
I chose Jackson because it is in my view the best JSON serializer/deserializer in the Java realm.
Having said all this, I once experimented writing services with my preferred Java REST framework, RESTEasy, in Scala. It was more awkward than my first date. Scala and RESTEasy just don't fit together because of an idiom mismatch, issues with types, and so on.
If you want to write REST services in Scala, please consider frameworks built with the language in mind like Scalatra, Unfiltered, or Spray.

JAX-RS client/server application using JSON and Wink

I'd like to implement JAX-RS server (on WebSphere Application Server) and client applications using JSON (Jackson convertor) format and Wink provider.
Do i need to use JAXB annotations for my DTO class that would be passed to and from the REST service and so must be serializable?
Which response type do i need to use, JSONObject or my Class type, i.e MyClass or String in Post methods negotiation between client/server?
In which cases we use JAXB annotations for domain classes in Rest services?
Your insight/directions would be high appreciated.
Thanks in advance,
Erwin
I guess you need to read more about Jackson.
But here are some short answers:
For 90% of cases you don't need JAXB annotations on your classes at all.
You need to use your classes:
#POST
public MyClass myMethod(MyOtherClass mcls)
You use JAXB annotations for some complex mapping, when you are not satisfied with the default results.
In addition to the above answer: it is often makes sense to use Jackson for JSON handling within Apache Wink. Jackson is more powerful and flexible than bundled facilities.
http://www.ibm.com/developerworks/java/library/wa-aj-jackson/index.html shows how to configure Apache Wink for Jackson.

Gson serializing Spring beans

I am using Gson 1.6 and Spring Framework 3.0 for a Java web app on WebSphere 6.1. I have some Spring beans for which the actual instance is a CGLIB proxy. When I attempt to serialize these beans via Gson, the non-primitive properties of the class are not serialized. Instead I get something like:
{
"CGLIB$BOUND":true,
"CGLIB$CONSTRUCTED":true,
"booleanProperty":true,
"anotherBooleanProperty":true,
}
where I was expecting something more like
{
"stringProperty":"stringValue"
"integerObjectProperty":17,
"booleanProperty":true,
"anotherBooleanProperty":true,
}
When I serialize a non-proxied POJO, the output is exactly as I'd expect. How can I get Gson to generate the output I expect?
I'd say your problem is the result of a bad practice.
Spring Beans are usually defined by behaviour, not state. And you should only serialize Classes that have State, not behaviour.
Refactor your code, transfer the state from the Beans to Value Objects, and serialize those.
I would consider trying out another JSON processor, Jackson (http://jackson.codehaus.org), since it has some support for dealing with cglib proxied objects. And Spring supports Jackson so you have less code to write, compared to gson-based version.

JBoss/JaxWS/JaxB: SOAP and JSON binding to one webservice

Hello.
I've written a SOAP-webservice using JAX-WS and JAX-B, running on a JBoss 5.
Due to the overhead of SOAP I now try to enable the access to the webservice via JSON (but it should still be available via SOAP).
My question now is the following:
Is it possible to use the same webservice class (with the, via JaxB defined, bean class as parameter) for both endpoints?
Can I also use JAX-B to parse the JSON input / output at the same bean as I use it for the SOAP transfer?
And if it is possible: How? Or where should I start?