JAXB编组之后的多个XMLRootElement

时间:2016-01-22 14:08:15

标签: jaxb

我有以下类与jax-ws webservice;

接口类(myserviceInt.java);

@WebService(name="myservice")
@SOAPBinding(style=Style.RPC)
public interface myserviceInt {
@WebMethod(action="urn:OutIn", operationName="ACCT")
public String getACCT(
        @WebParam(partName="Prod1")String prod1, 
        @WebParam(partName="Prod2")String prod2);

}

实现类(myserviceImpl.java);

@WebService(endpointInterface = "path.to.webservice.myserviceInt", portName="acct", serviceName="acctservice", targetNamespace="http://demo.acctservice.com/")
public class myserviceImpl implements myserviceInt {


@Override
public String getACCT(String prod1, String prod1){

Other codes

String id = "25";
String description = "The Third Product"
ACCTResponse acct = new ACCTResponse();

acct.setId(id);
acct.setDescription(description);
String XMLstring = acct.ACCTResponseBeanToXML(acct);
return XMLstring;

}

}

ACCTResponse Class(ACCTResponse.java);

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="ACCTResponse")
public class ACCTResponse {

    @XmlElement(name = "ID")
    private String id;
    @XmlElement(name = "Description")
    private String description;

... their setters and getters;

}

Marshaller Class(ACCTUtil.java);

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import path.to.responseclass.ACCTResponse;

public class ACCTUtil {


    public static String ACCTResponseBeanToXML(ACCTResponse acct)
    {
        String responseStr = null;
        try
        {
            StringWriter writer = new StringWriter();
            JAXBContext context = JAXBContext.newInstance(ACCTResponse.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(acct, writer);
            responseStr = writer.toString();
            int xmlHeaderIndex = responseStr.indexOf("?>");
            if(xmlHeaderIndex > 0)
                responseStr = responseStr.substring(xmlHeaderIndex + 2, responseStr.length());

        }
        catch(Exception ex){
            ex.printStackTrace();
        }

        return responseStr;
    }
}

所需的其他所有图书馆都被遗漏以节省时间。我还在ACCTResponse.java的相应路径中有package-info.java来添加所需的响应namespaceURI和前缀。

现在,如上所述,我在编组后得到以下作为回应;

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:ACCTResponse xmlns:ns2="http://demo.acctservice.com/">
         <return><![CDATA[<net:ACCTResponse xmlns:net="http://demo.acctservice.com/">
    <net:ID>25</net:ID>
    <net:Description>The Third Product</net:Description>
    </net:ACCTResponse>]]></return>
      </ns2:ACCTResponse>
   </S:Body>
</S:Envelope>

我希望为具有固定客户端的此Web服务的消费者实现的响应是这样的;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org /soap/envelope/">
   <saop:Body>
    <net:ACCTResponse xmlns:net="http://demo.acctservice.com/">    
    <net:ID>25</net:ID>
    <net:Description>The Third Product</net:Description>
    </net:ACCTResponse>
  </soapenv:Body>
</soapenv:Envelope>

我需要进行哪些修改/操作才能从我的代码中获得此响应。感谢。

0 个答案:

没有答案