i am using Jackson and was wondering if it is possible to set global properties using resources.xml?
Example:
In some places i need to do:
jsonMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
It would be nice that i can configure Jackson to do this global using spring resources.xml,
any suggestion on how to do this?
You can do this easily, just extend the org.codehaus.jackson.map.ObjectMapper class and specify all your properties in the constructor(or you can accept a map as well): Then autowire your own ObjectMapper where you need it.
Not out of the box -- but maybe someone could build such an extension, release as Jackson module?
Related
Is there are any way to instantiate an object of UploadedFile without using Mockito?
I've been searching but I only have found examples using Mockito. I'm trying to do Junits and I would like to avoid using it.
Thanks!
It is an interface, so if you don't want to use Mockito (or a similar mocking tool) just create your own class and implement the interface explicitly. There are not that many methods, so it is straightforward.
EDIT: or just use the public constructor in DefaultUploadedFile and go with that implementation.
I know there two ways to use the "Mock" and the "TestSubject" annotations with JUnit. The first one - is to specify the EasyMockLoader class object for the RunWith annotation for the class that contains fields marked by these annotations. The second one - is to mark the EasyMockRule field with the "Rule" annotation. How to use the "Mock" and the "TestSubject" annotations with TestNG ?
TestNG is not directly supported. But you can inject mocks using the annotations quite easily by doing
EasyMockSupport.injectMocks(this);
(from your test class)
As I known, EasyMock doesn't support TestNG out of the box but PowerMock does.
Maybe using PowerMock + EasyMock + TestNG will work like a charm.
Otherwise, about #Mock, you'll have to manage it by yourself (looking for fields, creating mock and injecting them) with a configuration method (a #BeforeX method) or an appropriate listener.
Another solution could be to use the Guice integration and making mocks in a Guice module.
Same solution for #TestSubject: configuration methods or listeners.
I'm trying to use SerializationConfig.Feature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS but I'm not configuring the mapper myself, relying on annotations exclusively and letting Spring's RestTemplate (de)serialize automatically. Is there a way to enable the aforementioned feature in this scenario (i.e. annotations only)?
NOTE: I'm using Jackson 1.x and can't upgrade due to other libs...
With JAX-RS (like DropWizard) you can actually annotated resource endpoints, using #JacksonFeatures
public class Resource {
#Path("item")
#GET
#JacksonFeatures(serializationEnable={ SerializationFeature.WRAP_ROOT_VALUE })
public Pojo getItem(String id) {
...
}
}
I don't know if Spring exposes similar functionality, but it seems possible it does. And if not, it is something they should be able to add to allow per-endpoint setting/clearing of SerializationFeatures / DeserializationFeatures. So if it is not available, maybe file a feature request for Spring project?
Yes, it is possible.
checkout this link: http://jackson.codehaus.org/1.7.0/javadoc/org/codehaus/jackson/map/annotate/JsonSerialize.html
Example:
#JsonSerialize(using=MySerializer.class,
as=MySubClass.class,
include=JsonSerialize.Inclusion.NON_NULL,
typing=JsonSerialize.Typing.STATIC
)
I have very simple scenario where class A registers instances for types.
A.register(T1.class, new H1());
A.register(T2.class, new H2());
this is fairly simple configuration when done by hand but guice injection doesn't work when I create instances outside the guice framework.
I try to figure out how to create and configure A with all instance with custom annotation using guice.
I have found something like this Scan the classpath for classes with custom annotation but it is not using guice.
thanks
so I guess code.google.com/p/google-guice/wiki/Multibindings is the only option so far that works, but it is not as nice as I would expect since you need to connect everything by hand.
Is there any way to instruct Jackson 2 ObjectMapper to use custom Classloader to deserialize JSON strings?
Here is the solution:
ClassLoader myClassLoader = ...
ObjectMapper om = new ObjectMapper();
TypeFactory tf = TypeFactory.defaultInstance()
.withClassLoader(myClassLoader);
om.setTypeFactory(tf);
Take a look at the Jackson Wiki for Modules, a Jackson Module can be used to create custom serialization / deserialization. While the wiki talks about what they can do
Add custom serializers and deserializers
Add mix-in annotations
Override or add AnnotationIntrospector
Access SerializationConfig and DeserializationConfig settings.
It is pretty light on details, just giving a very basic example. The Module JavaDoc is light on details as well. Fortunately the wiki also has a list of existing Jackson Modules on GitHub, which is more helpful as examples if you need to create your own Module from scratch.
To preserve the existing TypeMapper, you might want to do something like this (this is Scala, but same pattern works in Java), where you adjust the current type factory.
def updateClassLoader(cl: ClassLoader): Unit = {
objectMapper.setTypeFactory(objectMapper.getTypeFactory.withClassLoader(cl))
}
This will preserve cached type data from possible prior configuration calls like this:
mapper.registerModule(new Jdk8Module)
.registerModule(new JavaTimeModule)
.registerModule(new DefaultScalaModule)