Spring MVC calling webservice endpoint from the browser - rest

I want to be able to call my web service directly from the browser. Calling it from a HTTPClient via the web browser is OK, but if i try and call it directly via the browser I get the following error:
SEVERE: [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Could not instantiate JAXBContext for class [class com.mycompanay.MyClass]: 8 counts of IllegalAnnotationExceptions; nested exception is com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 8 counts of IllegalAnnotationExceptions
com.mycompany.MyInterface is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
com.mycompany.myInteface
at private java.util.List com.mycompanay.MyClass.values
at com.mycompanay.myClass
#XmlAttribute/#XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:
I've tried to reference the implmentation class via the #XMLElement annotation but i'm getting the same error:
public MyClass {
#XmlElement(type=MyInterfaceImpl.class, name="values")
private List<MyInterface> values;
#XmlElement(type=MyInterfaceImpl.class, name="values")
public void getValues() {
return values;
}
}

You were correct to use the #XmlElement annotation to specify the implementation type. The problem is still occurring because by default JAXB will treat public properties as mapped. This is why it is still trying to process the interfaces. Since you have annotated tr field you can add XmlAccessorType(XmlAccessType.FIELD) to your class.
For More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html

Related

EclipseLink-Moxy Unable to load custom DomHandler class while instantiating JAXB context

I am using Eclipselink Moxy Implementation of JAXB in my project to map complex XML to String Object using XmlAnyElement.
For this I have implemented DomHandler named as LayoutHandler.
I am using JAXB for resteasy web services deployed in JBoss 6.
I am facing Below issue intermittently -
Exception [EclipseLink-50033] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b):
org.eclipse.persistence.exceptions.JAXBException
Exception Description: The DomHandlerConverter for DomHandler
[com.**.LayoutHandler] set on property [layoutXml] could not be
initialized.
Internal Exception: java.lang.ClassNotFoundException:
com.**.LayoutHandler from
BaseClassLoader#5c0b3ad0{vfs:///*/*/jboss-server/server/all/deployers/resteasy.deployer}
While EclipseLink Moxy is instantiating JAXBContext using JAXBContext.newInstance(classes, properties)
After spending some time in debugging and analyzing the issue I could figure out that ClassLoader of resteasy is getting used to load LayoutHandler class instead of my application class loader(vfs://///jboss-server/server/all/deploy/app_name.ear/app_name.war/) which is causing the issue as its unable to find the LayoutHandler class.
When I bounce the server, issue is getting resolved so I am unable to find out the exact root cause. Any help will be appreciated.
Further debugging into org.eclipse.persistence.jaxb.JAXBContextFactory revealed that below two classes are getting passed to createContext() method of JAXBContextFactory -
org.jboss.resteasy.plugins.providers.jaxb.JaxbCollection
com.**.Model_class
public static javax.xml.bind.JAXBContext createContext(Class[] classesToBeBound, Map properties) throws JAXBException {
ClassLoader loader = null;
if (classesToBeBound.length > 0) {
loader = classesToBeBound[0].getClassLoader();
}
return createContext(classesToBeBound, properties, loader);
}
In above method classloader of first class is getting used to load the custom DomHandler later on.
When first element in array is model class at that time code is working fine as application context class loader is getting used but when the first element in array is JaxbCollection rest easy context class loader is getting used and its throwing mentioned exception.
This issue is occurring intermittently as order of elements in array is varying which might be due to the use of HashSet to hold the elements of type Class by caller of this method which is passing the classesToBeBound array
Note: I have replaced actual package names with *.
I'm surprised it works on a bounce... all of your JAXB bits need to line up, you should be using the moxy jaxb provider at all times. If it's failing after initial deploy, then working after a bounce, I suspect that you want to specify the moxy jaxb provider in your system properties ( -Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory ) and ensure that they're available to jboss when your app is not deployed.

Does Crud Repository has saveAll method that works?

I am trying to use CRUDRepository for my development project. I have seen in many posts that CRUD Repository do support saveAll method which allows to save a list of object in the database. But when I am using it, It is giving me an error that saveAll property is not found
Here is the detailed error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BinaryPartCRUDRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query method public abstract java.util.List xxx.xxx.xx.xxxx.repository.BinaryPartCRUDRepository.saveAll(java.util.List)! No property saveAll found for type BinaryPart!
Here is my code.
public interface BinaryPartCRUDRepository extends CrudRepository<BinaryPart, Long> {
BinaryPart save(BinaryPart binaryPart);
List<BinaryPart> saveAll(List<BinaryPart> binaryParts);
}
The save Function is working. But saveAll is not.
I have also tried to use the Persistence manager to do the batch save. But having null object while doing JUnit Testing. So I am preferring to stay with CRUD Repository. Appreciate any kind of suggestion.
saveAll already there in CrudRepository, so no need to specify your own method for save all in repository interface.
remove this part:
List<BinaryPart> saveAll(List<BinaryPart> binaryParts);
and in your service class , directly call `saveAll method. Remember this method using iterable as param and return value.
The saveAll method has the following signature:
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
You define an additional method with the same name, but a different signature. Spring Data does not know how to create an implementation for that and throws the exception.
Just change your interface to:
public interface BinaryPartCRUDRepository extends CrudRepository<BinaryPart, Long> {}
And you are good to go.

JPA EclipseLink Weaver generates call to porperty getter inside its setter -> NullPointerException

I have an #Embeddable class that uses property access to wrap another object that's not directly mappable by JPA via field access. It looks like this:
#Embeddable
#Access(AccessType.PROPERTY)
public class MyWrapper {
#NotNull
#Transient
private WrappedType wrappedField;
protected MyWrapper() {
}
public MyWrapper(WrappedType wrappedField) {
this.wrappedField = wrappedField;
}
#Transient
public WrappedType getWrappedField() {
return wrappedField;
}
public void setWrappedField(WrappedType wrappedField) {
this.wrappedField = wrappedField;
}
#Column(name = "wrappedTypeColumn")
protected String getJPARepresentation() {
return wrappedField.toString();
}
protected void setJPARepresentation(String jpaRepresentation) {
wrappedField = new WrappedType(jpaRepresentation);
}
}
Persisting an #Entity with a MyWrapper field works fine. But when I execute a query to load the Entity from the database, I get a NullPointerException. The stacktrace and some debugging shows that Eclipselink creates a new instance of MyWrapper by calling its default constructor and then calls the setJPARepresentation() method (as expected).
But now the unexpected happens: the stacktrace shows that the getJPARepresentation() is called from inside the setter, which then of course leads to a NullPointerException when return wrappedField.toString() is executed.
java.lang.NullPointerException
at MyWrapper.getJPARepresentation(MyWrapper.java:27)
at MyWrapper.setJPARepresentation(MyWrapper.java)
... 109 more
Fact is, there is obviously no call to the getter in the code and the stacktrace shows no line number indicating from where in the setter called the getter. So my conclusion would be, that the bytecode weaver of Eclipselink generated the call to the getter.
It's easy to build a workaround, but my question is: Why does Eclipselink do that?
P.S: I'm using EclipseLink 2.3.2.v20111125-r10461 in a GlassFish Server Open Source Edition 3.1.2 (build 23)
When weaving is enabled (default on Glassfish), EclipseLink will weave code into property get/set methods for,
change tracking
fetch groups (partial objects)
lazy (relationships)
For change tracking support the set method will be weaved to check if the new value is different than the old value, so it must call the get method to get the old value.
Now this is still odd, as since your are building a new object, I would not expect the change listener to be set yet, so would expect the change tracking check to be bypassed. You could decompile the code to see exactly what was generated.
The easiest fix is to just put in a null check in your get method, which is probably best in general for your code. You could also switch to field access, which will not have issues with side-affects in get/set methods. You could also use a Converter to handle the conversion, instead of doing the conversion in get/set methods.

GWT RequestContext ENum in the request

If we use enum as one of the attribute in the Request invocation , it throws an UnsupportedOpeationException and does not even invoke the service method on the server.
#Service(value = DesignService.class, locator = DesignServiceLocator.class)
public interface DesignRequest extends RequestContext {
Request<List<DesignProxy>> findDesign(SortEnum sortorder);
}
when we invoke the designRequest.findDesign(sortorderEnum).fire() the UnsupportOperationException is thrown on the javascript console on chrome dev tools/Firebug console.
Looks like it is related to Issue 6504, which will throw an UnsupportedOperationException if it fails to find the type you are using - consider trying to change to class methods in your enum, or wait until 2.4 is released.
If you are not using anonymous enum instances, can you post more info about this error, such as where the exception is thrown from?

cannot find my bean using the InitialContext.lookup() method

I have tried to use struts 1.3 API to make a small application with EJB 3.0. Unfortunatelly i cannot use the #EJB annotation to call my bean object from inside my action class. I have solved this problem using different workarounds ( the first one is to use my global jndi name of my bean and the other is to call another class first and use the #EJB annotation from that class). Still these two workarounds have significant disadvantages. I would like to call my EJB directly from my action class. I have read plenty examples using the "java:comp/env/beanName" JNDI name but still haven't figure out how to do it and get name not found axception.
Let the full name of the local EJB class be the com.ejb.myEjbPackage.MyEJBLocal, how can i call it using the context lookup? (can i do it without modifying any of the web.xml and sun-web.xml descriptors?)
I am using glassfish server and Netbeans IDE.
Thank you in advance
#EJB won't work in a standard pojo it can only be done in a managed object (i.e. another session bean)
So...
Here's your bean
#Stateless(mappedName="beanName")
public class beanName implements beanNameRemote {
Here's your lookup
Context context = new InitialContext(); //default lookup pulls from jndi properties file
context.lookup("beanName");
You can do some further reading on the mappedName to see if you want to use it or not.
I found the answer :
If you cannot use the EJB annotation in the class you want to call the bean then :
If you don't want to mess with XML descriptors to define your bean , you have to do it in the bean class itself.
Hence i used the following annotation in the GameBean class
#Stateless
#EJB(name="ejb/GameBean",beanInterface=GameBeanLocal.class,beanName="GameBean")
public class GameBean implements GameBeanLocal {.....
The beanName is optional. The annotation must be declared in the line ABOVE the declaration of the class.
Then, in order to call the bean from the other class you can do
InitialContext ic = new InitialContext();
ic.lookup("java:comp/env/ejb/GameBean");