需要jax-ws soap header示例,而不需要使用者创建一个SOAP处理程序来添加标头

时间:2014-03-13 23:31:38

标签: xsd wsdl jax-ws soapheader

我试图弄清楚如何将请求标头放入wsdl。如果我在下面做了任何错误的事情,请告诉我,因为这将由另一个团队使用,如果有任何错误,他们可能会出现问题!

我从一个借用异常处理的例子开始。

这是我的界面:

package com.companyname.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface MyService {

    @WebMethod
    public String greetMe(
                    RequestHeader requestHeader,
                    String name
                    ) throws MyServiceException;
}

这是我的实现类:

package com.companyname.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public class MyServiceImpl implements MyService {
    public MyServiceImpl() {
    }

    @WebMethod
    public String greetMe(
                    @WebParam(name="requestHeader",header=true)RequestHeader requestHeader,
                    @WebParam(name="name")String name
                    ) throws MyServiceException {
        if (name.equalsIgnoreCase("fault")) {
            MyServiceFault fault = new MyServiceFault();
            fault.setFaultCode("1234");
            fault.setFaultString("My Service Error");
            throw new MyServiceException("1234", "My Service Error");
        } else {
            return "Hello " + name;
        }
    }   
}

这是RequestHeader类:

package com.companyname.ws;

public class RequestHeader {

    private String requestId;

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

}

这是wsdl:

<?xml version='1.0' encoding='UTF-8'?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.companyname.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.companyname.com/" name="MyServiceImplService">
    <types>
        <xsd:schema>
            <xsd:import namespace="http://ws.companyname.com/" schemaLocation="http://localhost:8081/MySoapFaultExample/myservice?xsd=1"/>
        </xsd:schema>
    </types>
    <message name="greetMe">
        <part name="parameters" element="tns:greetMe"/>
        <part name="requestHeader" element="tns:requestHeader"/>
    </message>
    <message name="greetMeResponse">
        <part name="result" element="tns:greetMeResponse"/>
    </message>
    <message name="MyServiceException">
        <part name="fault" element="tns:MyServiceFault"/>
    </message>
    <portType name="MyServiceImpl">
        <operation name="greetMe" parameterOrder="parameters requestHeader">
            <input wsam:Action="http://ws.companyname.com/MyServiceImpl/greetMeRequest" message="tns:greetMe"/>
            <output wsam:Action="http://ws.companyname.com/MyServiceImpl/greetMeResponse" message="tns:greetMeResponse"/>
            <fault message="tns:MyServiceException" name="MyServiceException" wsam:Action="http://ws.companyname.com/MyServiceImpl/greetMe/Fault/MyServiceException"/>
        </operation>
    </portType>
    <binding name="MyServiceImplPortBinding" type="tns:MyServiceImpl">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="greetMe">
            <soap:operation soapAction=""/>
            <input>
                <soap:body use="literal" parts="parameters"/>
                <soap:header message="tns:greetMe" part="requestHeader" use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
            <fault name="MyServiceException">
                <soap:fault name="MyServiceException" use="literal"/>
            </fault>
        </operation>
    </binding>
    <service name="MyServiceImplService">
        <port name="MyServiceImplPort" binding="tns:MyServiceImplPortBinding">
            <soap:address location="http://localhost:8081/MySoapFaultExample/myservice"/>
        </port>
    </service>
</definitions>

我尝试使用myeclipse生成客户端类,但是我在WSDL验证期间遇到了这个错误:

WSDL file has validation errors. Code generation may fail

WSDL URL: http://localhost:8081/MySoapFaultExample/myserver?wsdl

The part 'requestHeader' has an invalid value 'requestHeader' defined for its element. Element declarations must refer to valid values defined in a schema.

我仍然点击了Finish,然后生成了类。但我担心我做错了什么。我该怎么做才能避免验证错误?另外,另一个问题是:我必须在某处找到实际的物理xsd文件吗?我应该用wsgen生成它们吗?我不太了解这个验证步骤,好像它希望xsd文件在哪里验证?如果我错了,请纠正我。

这是我的测试客户端:

package com.companyname.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.Handler;

import com.companyname.handler.LoggingHandler;
import com.companyname.ws.GreetMe;
import com.companyname.ws.MyServiceException;
import com.companyname.ws.MyServiceImpl;
import com.companyname.ws.RequestHeader;

public class Client {

    private static final String WS_URL = "http://localhost:8081/MySoapFaultExample/myservice?wsdl";
    private static final String NAMESPACE_URI = "http://ws.companyname.com/";
    private static final String LOCAL_PART = "MyServiceImplService";

    public static void main(String[] args) {
        URL url = null;
        try {
            url = new URL(WS_URL);
        } catch (MalformedURLException mue) {
            System.out.println("MalformedURLException: " + mue.toString());
            return;
        }
        QName qname = new QName(NAMESPACE_URI, LOCAL_PART);
        Service service = Service.create(url, qname);
        MyServiceImpl myServiceImpl = service.getPort(MyServiceImpl.class);
        BindingProvider bindingProvider = (BindingProvider) myServiceImpl;
        Binding binding = bindingProvider.getBinding();
        List<Handler> handlerList = binding.getHandlerChain();
        handlerList.add(new LoggingHandler());
        bindingProvider.getBinding().setHandlerChain(handlerList);
        try {
            RequestHeader requestHeader = new RequestHeader();
            requestHeader.setRequestId("1");
            GreetMe greetMe = new GreetMe();
            greetMe.setName("javagirl");
            myServiceImpl.greetMe(greetMe, requestHeader);
        } catch (MyServiceException mse) {
            System.out.println("Fault Code: " + mse.getFaultInfo().getFaultCode());
            System.out.println("Fault String: " + mse.getFaultInfo().getFaultString());
            return;
        }
    }
}

我有一个日志记录处理程序来记录原始客户端请求和从服务器返回的响应:

***************************************
original client request
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header>
        <ns2:requestHeader xmlns:ns2="http://ws.companyname.com/">
            <requestId>1</requestId>
        </ns2:requestHeader>
    </S:Header>
    <S:Body>
        <ns2:greetMe xmlns:ns2="http://ws.companyname.com/">
            <name>javagirl</name>
        </ns2:greetMe>
    </S:Body>
</S:Envelope>

***************************************
response returned from server
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header/>
    <S:Body>
        <ns2:greetMeResponse xmlns:ns2="http://ws.companyname.com/">
            <return>Hello javagirl</return>
        </ns2:greetMeResponse>
    </S:Body>
</S:Envelope>

关于请求标头,greetMe和greetMeResponse的上述名称空间的另一个问题。他们都是一样的是错的吗?:

http://ws.companyname.com/

或者每个人都应该不同?

0 个答案:

没有答案