I am new to JAX-WS, when I try to start this simple Web Service:
Interface:
package ws;
import java.util.HashMap;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface IBasket {
@WebMethod public abstract void addItem(int productId, int count);
@WebMethod public abstract HashMap<Integer, Integer> getBasketMap();
}
Implementing Class:
package model;
import java.util.HashMap;
import javax.jws.WebService;
import ws.IBasket;
@WebService(endpointInterface = "ws.IBasket")
public class Basket implements IBasket {
private HashMap<Integer, Integer> basket;
public Basket() {
this.basket = new HashMap<Integer, Integer>();
}
@Override
public void addItem(int productId, int count) {
int currentCount = 0;
if (basket.containsKey(productId)) {
currentCount = basket.get(productId);
}
basket.put(productId, currentCount + count);
}
@Override
public HashMap<Integer, Integer> getBasketMap() {
return basket;
}
}
Publisher:
package endpoint;
import javax.xml.ws.Endpoint;
import model.Basket;
public class WSPublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/WS/Basket",new Basket());
}
}
I'm getting the following stacktrace:
Exception in thread "main" javax.xml.ws.WebServiceException: class ws.jaxws.GetBasketMapResponse do not have a property of the name return
at com.sun.xml.internal.ws.server.sei.EndpointResponseMessageBuilder$DocLit.<init>(EndpointResponseMessageBuilder.java:196)
at com.sun.xml.internal.ws.server.sei.EndpointMethodHandler.createResponseMessageBuilder(EndpointMethodHandler.java:191)
at com.sun.xml.internal.ws.server.sei.EndpointMethodHandler.<init>(EndpointMethodHandler.java:97)
at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.<init>(SEIInvokerTube.java:72)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:208)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:498)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:246)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:170)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:113)
at javax.xml.ws.Endpoint.publish(Endpoint.java:240)
at endpoint.WSPublisher.main(WSPublisher.java:14)
Caused by: javax.xml.bind.JAXBException: return is not a valid property on class ws.jaxws.GetBasketMapResponse
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getElementPropertyAccessor(JAXBContextImpl.java:966)
at com.sun.xml.internal.ws.server.sei.EndpointResponseMessageBuilder$DocLit.<init>(EndpointResponseMessageBuilder.java:193)
... 10 more
Any ideas how to solve this?
Ok, I've found the solution for this problem, see:
How can I pass in an array as a value into a PHP soapclient request?
Its the same for HashMap return values. JAXB can't handle HashMaps as parameters/return values, maybe because they're generic.
So instead, we need a wrapper class for the HashMap<Integer, Integer>
public class HashMapWrapper {
private HashMap<Integer, Integer> basketMap;
public HashMapWrapper(HashMap<Integer, Integer> basketMap) {
this.setBasketMap(basketMap);
}
and use this one as return value.
Are you using Metro as your JAX-WS provider? If so try setting the Document style as follows to RPC. This annotation should come after the @WebService annotation.;
@SOAPBinding(style = Style.RPC)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.