How do I get proguard working with sjersey? - json

Trying to use Jersey with Scala, via a fork of SJersey, and obfuscating it with ProGuard.
I've got all of this stuff:
-keepattributes SourceFile,LineNumberTable,*Annotation*,EnclosingMethod,Signature,InnerClasses
and I'm doing this:
-keep public class com.example.*JsonSeralisedClasses {
public *;
}
but when I encode stuff with jersey nothing comes out!

Figured out eventually that SJersey doesn't use public methods, but introspects onto private fields, so one needs to do this:
-keep public class com.example.*JsonSeralisedClasses {
public protected private *;
}
See protected private is the difference.
Alernatively, you can annotate everything with #BeanProperty (which makes public accessors that are preserved using the original config).

Related

Does Mockito support #Any #Inject Instance<> interface?

I have a init() method that use injected private instance<>. How can I test this class using JUnit or Mockito? I tried to make some fake class and add them to a list and set this list to my private field but I have this error
java.lang.IllegalArgumentException: Can not set javax.enterprise.inject.Instance field ......
MyClass is:
#Singleton
#Startup
public class HandlerManager {
#Any
#Inject
private Instance<RollbackHandler<RollbackData>> handlers;
private RollbackHandler<RollbackData> rollbackHandler;
#PostConstruct
public void init() {
for (RollbackHandler<RollbackData> bean : handlers) {
//do something
}
}
}
Any annotation is not processed by frameworks, unless you use custom work. you will have to define all those dependencies as mocks in your test using #Mock and call injectMocks() from before test methods such as setup(). It is a multi part problem.
Use constructor injection, field injection is evil. you still will be able to annotate your constructor with #Inject.
when(provider.iterator()).thenReturn(list.iterator);
works for me.
You can create a temporary list with concrete implementations of the RollbackHandler, and mock the iterator() method of your Instance<RollbackHandler<RollbackData>> object so that it returns the iterator of the temporary list.
Example:
private void mockIterator() {
Instance<RollbackHandler<RollbackData>> handlers = mock(Instance.class);
List<RollbackHandler<RollbackData>> handlersList = Collections.singletonList(new RollbackHandlerImpl<>());
when(handlers.iterator()).thenReturn(handlersList.iterator());
}

java.util.Collection deserialization using jackson

I have a problem deserializing Collection. Please help me. Below there are 3 VO's and the first UpdateFiltersForQueueUserIdByVO is getting passed to my REST method. If you see the inside vo's are old one's and 1.4 is used to compile them and they have generic Colleciton properties. json is unable to deserialize the Collection. How can we define mixin for FilterProfileVO, FilterVO. Thanks in advance.
// This is compiled in java 1.7, wrapper vo
public class UpdateFiltersForQueueUserIdByVO {
private FilterProfileVO filterProfileVO;
}
// Below two vo's are getting compiled in java 1.4 and I can't change
public class FilterProfileVO extends ValueObject implements Serializable {
// some other variables
private Collection filterVOs;
}
public class FilterVO extends ValueObject{
private Collection filterDetailsList;
private Collection filterCodeValueColl;
}
Regarads,
GP
Issue resolved after implementing below two mixin classes. Hope this helps someone.
public interface FilterProfileVOMixin {
#JsonSerialize(as=ArrayList.class, contentAs=FilterVO.class)
#JsonDeserialize(as=ArrayList.class, contentAs=FilterVO.class)
public Collection getFilterVOs();
}
public interface FilterProfileVOMixin {
#JsonSerialize(as=ArrayList.class, contentAs=FilterVO.class)
#JsonDeserialize(as=ArrayList.class, contentAs=FilterVO.class)
public Collection getFilterVOs();
}

make #jsonignore use a setter over a isMethod?

This is my class
public class HouseJPAImpl implements House {
public RoomJPAImpl room;
public RoomJPAImpl getRoom(){
return this.room;
}
public void setRoom(RoomJPAImpl room){
this.room = room;
}
#Override
public boolean isRoom(){
return false;
}
}
My code gets confused with getRoom and isRoom.
Caused by: java.lang.IllegalArgumentException: Conflicting getter definitions for property "room": com.shared.model.restimpl.jpa.HouseJPAImpl#getRoom(0 params) vs com.shared.model.restimpl.jpa.HouseJPAImpl#isRoom(0 params)
I tried putting the #jsonignore on the isRoom method but then i dont get the room property in JSON.
Is there a way to use the getRoom over isRoom?
First of all, this is something that Jackson 2.3 will handle gracefully (see https://github.com/FasterXML/jackson-databind/issues/238).
But until it gets released, there are 2 main ways to handle this:
Add #JsonIgnore on isRoom(), but keep #JsonProperty on getRoom()
Change visibility settings to filter out all isXxx() methods: can either set global settings (ObjectMapper has something like setVisibility), or use annotation #JsonAutoDetect on classes
If this is an isolated case, you are probably better off by just using first one.

Jaxb json missing brackets for one element array

I'm using JAXB/Jersey (1.3) to convert java to json in a REST API.
I read a lot about this problem, I tryed this solution, it work a half:
#XmlRootElement
public class ArrayWrapper
{
public List<String> list = new LinkedList<String>();
}
and my ContextResolver:
#Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = {ArrayWrapper.class,Wrapper.class};
public JAXBContextResolver() throws Exception {
MappedBuilder builder = JSONConfiguration.mapped();
builder.arrays("list");
builder.rootUnwrapping(true);
this.context = new JSONJAXBContext(builder.build(), types);
}
ArrayWrapper aw=new ArrayWrapper();
aw.list.add("test");
I get {"list":["test"]} so it works but when I wrapp ArrayWrapper in an other class it don't work:
#XmlRootElement
public class Wrapper
{
public ArrayWrapper aw;
public Wrapper()
{
aw=new ArrayWrapper();
aw.list.add("test");
}
}
new Wrapper();
I get {"aw":{"list":"test"}}
Anyone know how to fix it?
I am not quite sure if you got it working so I am contributing my bit.
I also stumbled upon this issue recently. I found a post in stackoverflow that helped me, but even more helpful was this article (introducing Jackson might help).
I hope this helps you, too. For me it was a matter of 5 minutes to fix the issue.

Struts2 JSON Plugin With Annotations

I have a Struts2 Action Class configured via annotations. All of the "normal" methods that are annotated with #Action work fine.
However, I need to add a method into the action that returns JSON.
Here is a trimmed down version of my class (dao autowired with Spring):
#Namespace("featureClass")
// define success and input actions for class here
public class FeatureClassAction extends ActionSupport {
FeatureClassDao featureClassDao;
#Autowired
public setFeatureClassDao(FeatureClassDeao featureClassDao) {
this.featureClassDao = featureClassDao;
}
List<FeatureClass> featureClasses;
// snip normal actions
#Action("/featureClassesJSON")
#JSON
public String getFeatureClassesJSON() throws Exception {
featureClasses = featureClassDao.getAll();
return SUCCESS;
}
}
Can anyone assist? If I have to go the struts.xml route, that means moving all of my other actions (which work fine) into it.
I figured I would share the answer, since anyone else with the same problem would likely also face the silence.
I created two actions: FeatureClassAction and FeatureClassJsonAction. FeatureClassAction was annotated as such:
#ParentPackage("struts-default")
#Namespace("/featureClass")
public class FeatureClassAction extends ActionSupport {
FeatureClassJsonAction is annotated like this:
#ParentPackage("json-default")
#Namespace("/featureClass")
public class FeatureClassJsonAction extends ActionSupport {
The method in the JSON Action was annotated like this:
#Action(value="featureClassesJson", results = {
#Result(name="success", type="json")
})
public String getFeatureClassesJSON() throws Exception {
Hope it helps someone.