I am creating a Junit Test using CamelTestSupport. The Camel Route has few references to POJO Beans and OSGI references.The OSGI reference is an Interface.I had used SimpleRegistry to capture the POJO beans using Java DSL. However, I do not know how to add OSGI reference or OSGIServiceRegistry for the purpose.
How do I had OSGI reference and POJO Beans along with the OSGI references in the registry so that CamelTestSupport can trigger the test method?
For reference-
Camel version - 2.13.2
Junit version - 4.11
If you use blueprint, you can try to use CamelBlueprintTestSupport rather than CamelTestSupport. If it does not help, you can look at the code of CamelBlueprintTestSupport, specially the createBundleContext() method, wich registers some services at startup.
Related
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?
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?
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.
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.
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.