1

当我尝试启动这个简单的 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

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

4

2 回答 2

7

好的,我已经找到了解决此问题的方法,请参阅:

如何将数组作为值传递给 PHP soapclient 请求?

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

因此,我们需要一个 HashMap<Integer, Integer> 的包装类

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

并将其用作返回值。

于 2012-12-09T11:41:16.627 回答
2

您是否使用 Metro 作为您的 JAX-WS 提供程序?如果是这样,请尝试将 Document 样式设置为 RPC,如下所示。这个注解应该在@WebService 注解之后。

@SOAPBinding(style = Style.RPC)
于 2012-12-09T01:15:57.170 回答