为Soap Web服务抛出错误代码500和404的自定义消息

时间:2018-07-02 18:31:34

标签: java soap jax-ws soapfault custom-error-handling

我需要使用解码后的字符串将文件保存在上述文件路径中,一切正常,但是我担心的是返回错误代码500和404的自定义消息

这是Web服务:

@WebService(serviceName = "NewWebService")   
public class NewWebService {   
@WebMethod(operationName = "resFileUpload")    
public String resFileUpload(@WebParam(name = "fileName") String fileName, @WebParam(name = "Base64EncFile") String Base64EncFile) {   
        String filePath = "D:\\response\\" + fileName;   
        try {    
            FileOutputStream fos = new FileOutputStream(filePath);    
            BufferedOutputStream outputStream = new BufferedOutputStream(fos);    
            outputStream.write(decodeString.getBytes());     
            outputStream.close();    
}catch (IOException ex) {}    

这是我的肥皂发布请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tr="http://tr.jp.com/">
<soapenv:Body>
<tr:resFileUpload>
<fileName>test.xml</fileName>
<Base64EncFile>encodedinput</Base64EncFile>
</tr:resFileUpload>
</soapenv:Body>
</soapenv:Envelope>

成功的结果

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:resFileUploadResponse xmlns:ns2="http://tr.jp.com/">
            <return>Server Error</return>
        </ns2:resFileUploadResponse>
    </S:Body>
</S:Envelope>

故障案例

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/test" xmlns:tr="http://tr.jp.com/">
 <soapenv:Body>
<tr:resFileUpload>
<fileName>test.xml</fileName>
<Base64EncFile>encodedinput</Base64EncFile>
</tr:resFileUpload>
</soapenv:Body>
</soapenv:Envelope>

结果:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
            <faultcode>S:VersionMismatch</faultcode>
            <faultstring>Couldn't create SOAP message. Expecting Envelope in namespace http://schemas.xmlsoap.org/soap/envelope/, but got http://schemas.xmlsoap.org/soap/envelope/testdb </faultstring>
        </S:Fault>
    </S:Body>
</S:Envelope>

为此,我需要生成自定义消息,并且如果我在任何输入字段中将'&'作为输入参数传递,那么我将得到类似

<faultcode>S:Server</faultcode>
<faultstring>[com.ctc.wstx.exc.WstxLazyException] Unexpected character '&lt;' (code 60); expected a semi-colon after the reference for entity 'pt.xml'&#xD;
 at [row,col {unknown-source}]: [4,20]</faultstring>

我知道它实际上是因为xml格式无效,为了使其正确,我们需要传递'&amp;而不是'&'

我已经尝试了很多,但是找不到任何解决方案,请帮助我返回错误代码500和404的自定义消息

0 个答案:

没有答案