web service - client classes - java

The web service that I implemented is up and running, when I try to run the client I get the following error with regard to the classes that were generated using wsimport,
Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://server.agency.hw2/}userJoined". Use #XmlType.name and #XmlType.namespace to assign different names to them.
this problem is related to the following location:
at hw2.chat.backend.main.generatedfromserver.UserJoined
at public javax.xml.bind.JAXBElement hw2.chat.backend.main.generatedfromserver.ObjectFactory.createUserJoined(hw2.chat.backend.main.generatedfromserver.UserJoined)
at hw2.chat.backend.main.generatedfromserver.ObjectFactory
this problem is related to the following location:
at ChatCompany.BackendChatServer.hw2.chat.backend.main.generatedfromserver.UserJoined
Two classes have the same XML type name "{http://server.agency.hw2/}userJoinedResponse". Use #XmlType.name and #XmlType.namespace to assign different names to them.
this problem is related to the following location:
at hw2.chat.backend.main.generatedfromserver.UserJoinedResponse
at public javax.xml.bind.JAXBElement hw2.chat.backend.main.generatedfromserver.ObjectFactory.createUserJoinedResponse(hw2.chat.backend.main.generatedfromserver.UserJoinedResponse)
But I can't figure out what exactly is meant by the error. I am assuming I need to change something in annotations in these classes as pointed out by the compiler:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "userJoinedResponse")
public class UserJoinedResponse {
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "userJoined", propOrder = {
"arg0"
})
public class UserJoined {
could someone please point out why there's a name collision and what annotations I need to change?
thanks

My teacher has suggested (among other reasons) that maybe there is something wrong with the way i ran the wsimport utility, so I regenerated the client class files using wsimport and the problem was solved. it might have been that I didn't specify the package name at the beginning and did it manually first time but I am uncertain.

I got the same exception. The reason is a similarity of names both classes. When you have class with name UserJoined during wsdl generation process JAXB generate element with name UserJoinedResponse (see your wsdl file).
And the you try to add another class with the same name (UserJoinedResponse). So just rename UserJoinedResponse class or annotate it as mentioned in the exception.

Related

Obtaining the fully qualified name from an Annotation using Eclipse JDT AST

I am using the Eclipse JDT to build AST for Java source code, so I can do some code analysis. Currently I would like to obtain the fully qualified name of an annotation. Consider the code below:
import javax.persistence.Entity;
#Entity
public class Class1
If I visit this Compilation Unit, the #Entity is a MarkerAnnotation. And I can do some analysis on it. However I am unable to obtain the Fully qualified name. I would like to obtain "javax.persistence.Entiy". I have tried several ways, but with no success.
public boolean visit(MarkerAnnotation node) {
node.getTypeName(); //returns the simple name
node.getTypeName().getFullyQualifiedName();// I thought this would print javax.persistence.Entiy,
// but it only prints "Entity"
node.resolveTypeBinding().getName(); //Prints "Entity"
node.resolveTypeBinding().getBinaryName(); // Prints "Entity"
node.resolveAnnotationBinding().getName(); //Prints "Entity"
return super.visit(node);
}
I have also tried to cast MarkerAnnotation to Annotation, but I am still unable to get fully qualified name. During debugging sesssions, I had no success either navigating this node
I was able to get the fully qualified name using the imports() method of the CompilationUnit. I did some String manipulations on them, combining with the annotations simple name. However, I feel this is sort of hacky, and I need to look at every import, even ones that are not related to annotations.
What I would like is to obtain the fully qualified name directly from the node, i.e, from the MarkerAnnotation, NormalAnnotation and SingleMemberAnnotation. Is there any way to achieve this? What Am I missing here?
Thanks in advance!
From the javadoc of Annotation.resolveAnnotationBinding():
Note that bindings (which includes resolved annotations) are generally unavailable unless requested when the AST is being built.
So please check how you configure ASTParser, see ASTParser.setResolveBindings(boolean)

Bazel build with Jackson annotations for ser/Deser and generate Java classes

So, we have been trying to xploring bazel as a build system for our organization, since we have a huge monorepo.
One of the problems I'm facing is, we have some code-gen classes which use Jackson's annotation processors to generate immutable copies of some file types.
Eg :
#JsonSerialze
#JsonInclude(JsonInclude.Include.NON_EMPTY)
#Value.Immutable
#JsonDeserialize(as=ImmutableABC.class)
Public abstract class ABC {
...
}
So, for this , I include a java_plugin tag in bazel build file for this module as follows :
Java_plugin(
name="abcgen",
srcs=["src/.../ABC.java"],
Deps=[ {jackson-deps go here}],
processor_class = "org.immutables.processor.ProxyProcessor",
)
This always fails , saying cannot find the ImmutableABC.class file which is referenced in the annotation.
Any ideas? Am I missing the processor class for the Jackson annotations, and also is it possible to include multiple processor classes?
For anyone facing issues as such, ensure generates_api = 1 for your plugin, if the generated classes are used in the library, as for the jackson part, that wasn't really the problem

JAXB Exception: Class not known to this context while marshalling

I am getting below error while marshalling. I generated xml's from schemas.
[javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: class org.com.AccountsComplexType nor any of its super class is known to this context.
After a little research, some people says "Try adding #XmlSeeAlso(...class)" but the problem is AccountsComplexType class is not a XmlRootElement.
Is there any idea what am I doing wrong?
If you generated the classes form an XML schema then you should bootstrap the JAXBContext from the package name of the generated model or the ObjectFactory class.

jaxb.index and nested classes (and OSGi)

When I try to refer to nested classes within my jaxb.index file, an exception is thrown during serialization. How can this be avoided?
This is in an Eclipse RCP application. The classes causing the exception are in a different plug-in than the one that creates the JAXB context and initiates serialization. The classes are in one of the plug-in's exported packages.
The class structure looks like this (names have been changed):
#XmlRootElement(name="foo")
#XmlAccessorType (XmlAccessType.FIELD)
public class Foo extends AbstractFoo {
...
#XmlRootElement(name="fooMetric")
#XmlAccessorType (XmlAccessType.FIELD)
public static class FooMetric implements IFooMetric {
...
}
}
The jaxb.index file contains these:
Foo
Foo.FooMetric
During serialization, the exception says to use "OuterClass.InnerClass" -- which I'm doing.
javax.xml.bind.JAXBException: error loading class "Foo.FooMetric" listed in com/mypackage/jaxb.index, make sure that entries are accessable on CLASSPATH and of the form "ClassName" or "OuterClass.InnerClass", not "ClassName.class" or "fully.qualified.ClassName"
- with linked exception:
[java.lang.ClassNotFoundException: com.mypackage.Foo.FooMetric]
The javadocs ("Format for jaxb.index") also suggests that jaxb.index can contain entries of the form OuterClass.InnerClass.
Constraints on class name occuring in a jaxb.index file are:
Must not end with ".class".
Class names are resolved relative to package containing jaxb.index file. Only classes occuring directly in package containing jaxb.index file are allowed.
Fully qualified class names are not allowed. A qualified class name,relative to current package, is only allowed to specify a nested or inner class.
However, this does not appear to work. What will make it work?
The solution I found (by trial and error) was to use OuterClass$InnerClass in jaxb.index instead of OuterClass.InnerClass. This allows serialization to complete successfully.
However, I haven't found any authoritative source that recommends this.
[I'm posting this solution per stackoverflow guidelines, but would love to see and accept a better answer.]

WSImports specify package name for a class

I have following scenario, I have two packages say com.ws.a and com.ws.b.
Both the packages having web-services classes. com.ws.a contain class TestServoceA which is having method which returns class TestA.
I want to pass this generated class to TestServiceB which is in com.ws.b.
But wsimport tool generate two different classes for both packages like:
com.ws.a.TestA and
com.ws.b.TestA
I want to generate only one class com.ws.a.TestA as a proxy and use it in both namespaces.
How can I do this? If custom binding can help me please give me example.
Thanks.
wsimport will use JAXB for data binding, follow this documentation to specify custom bindings:
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html

Categories

Resources