JAX-WS异常 - “return不是有效的属性”

时间:2012-12-08 22:57:29

标签: java soap jax-ws

当我尝试启动这个简单的Web服务时,我是JAX-WS的新手:

接口:

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();
}

实施班级:

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;
    }
}

出版商:

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());
    }
}

我收到了以下的堆栈跟踪:

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

任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:7)

好的,我已经解决了这个问题,请参阅:

How can I pass in an array as a value into a PHP soapclient request?

对于HashMap返回值也是如此。 JAXB无法将HashMaps作为参数/返回值处理,可能是因为它们是通用的。

相反,我们需要一个HashMap的包装类

public class HashMapWrapper {

    private HashMap<Integer, Integer> basketMap;

    public HashMapWrapper(HashMap<Integer, Integer> basketMap) {
        this.setBasketMap(basketMap);
    }

并使用此值作为返回值。

答案 1 :(得分:1)

您使用Metro作为JAX-WS提供商吗?如果是这样,请尝试按如下方式将文档样式设置为RPC。这个注释应该在@WebService注释之后。;

@SOAPBinding(style = Style.RPC)