Hi all,
It seems that XmlJavaTypeAdapters are not taken into account when
unmarshalling from xs:anyType
I have an Object i don't want to modify (I can't add annotations or add a
public constructor) :
public class Value {
private String value1;
private String value2;
public Value(String value1, String value2) {this.value1 =
value1;this.value2 = value2;}
public String getValue1() { return value1; }
public String getValue2() { return value2; }
public String toString() { return "{"+value1+","+value2+"}";}
}
So I added an adapter :
public class ValueAdapter extends XmlAdapter<ValueType, Value> {
@Override
public ValueType marshal(Value value) throws Exception {
ValueType valueType = new ValueType();
valueType.value = value.getValue1()+":"+value.getValue2();
return valueType;
}
@Override
public Value unmarshal(ValueType valueType) throws Exception {
String[] values = valueType.value.split(":");
return new Value(values[0], values[1]);
}
}
that is declared in package-info.java :
@XmlJavaTypeAdapter(value = ValueAdapter.class, type = Value.class)
package test;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
Here is the interface of my service :
@WebService
public interface MyService {
public void setValue(@XmlJavaTypeAdapter(ValueAdapter.class) Value
value);
public void setObject(Object object);
public void setEntry(Entry entry);
}
where Entry is :
@XmlRootElement
@XmlType
@XmlSeeAlso(ValueType.class)
public class Entry {
@XmlElement
String name;
@XmlElement
Object value;
@Override
public String toString() {
return name+"="+value;
}
}
The implementation of MyService is quite simple :
@WebService(endpointInterface = "test.MyService",
serviceName = "MyService")
public class MyServiceImpl implements MyService {
public void setValue(Value value) { System.out.println(value);}
public void setEntry(Entry entry) { System.out.println(entry); }
public void setObject(Object object) { System.out.println(object);}
}
Here is the server (nothing special here)
public class Server {
protected Server() throws Exception {
System.out.println("Starting Server");
MyServiceImpl implementor = new MyServiceImpl();
String address = "http://localhost:9000/myService";
Endpoint ep = Endpoint.publish(address, implementor);
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(30 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
And here is the client :
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://test/", "MyService");
private static final QName PORT_NAME
= new QName("http://test/", "MyServicePort");
private Client() {
}
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:9000/myService";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
endpointAddress);
MyService hw = service.getPort(MyService.class);
hw.setEntry(null);
Entry entry = new Entry();
entry.name = "name";
entry.value = "value";
hw.setEntry(entry);
hw.setValue(new Value("value1", "value2"));
// this does not work
// Value value = new Value("value1", "value2");
// hw.setObject(value);
// this does not work either
entry = new Entry();
entry.name = "name2";
entry.value = new Value("value1", "value2");
hw.setEntry(entry);
}
}
1)
@XmlJavaTypeAdapter(ValueAdapter.class) is needed in MyService.setValue :
public void setValue(@XmlJavaTypeAdapter(ValueAdapter.class) Value value);
This is a bit strange as I declared it in package-info.java.
(this is not a real problem for me however)
2)
Value value = new Value("value1", "value2");
hw.setObject(value);
throws an exception :
org.apache.cxf.interceptor.Fault: Marshalling Error: class test.Value nor
any of its super class is known to this context.
at
org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:174)
Sure it does not know Value but it knows how to adapt Value to ValueType ...
3)
entry = new Entry();
entry.name = "name2";
entry.value = new Value("value1", "value2");
hw.setEntry(entry);
Same problem here ...
What can I do ? (my biggest concern is for case 3)
Thanks !
--
View this message in context:
http://www.nabble.com/problem-with-%40XmlJavaTypeAdapter-and-xs%3AanyType-tp19624451p19624451.html
Sent from the cxf-user mailing list archive at Nabble.com.