簡體   English   中英

使用JAXB的jersey webservices中的xml語法無效

[英]invalid xml syntax in jersey webservices using JAXB

我正在使用jersey Web服務,該服務使用POST請求作為xml並產生響應作為xml。 我正在使用JAXB將XML編組/解組為Java bean。 這里的“ TestCall”是一個存儲請求中指定的所有屬性的類,但是如果請求中XML格式不正確(即缺少結束標記),我需要在響應XML中返回錯誤代碼。 那么如何處理這些情況呢?

這是我的代碼:

@POST 
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/testCall")
@Override
public TestCallOutput postCall(TestCall testCall) 
    return myService.acceptCallData(testCall);
}

我使用以下2個類來編組xml:

import javax.xml.bind.annotation.XmlRegistry;
@XmlRegistry
public class ObjectFactory {


/**
 * Create a new ObjectFactory that can be used to create new instances of schema 
 * 
 */
public ObjectFactory() {
}

/**
 * Create an instance of {@link TestCall }
 * 
 */
public Ivrcall createTestcall() {
    return new Testcall();
}

}

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {

})
@XmlRootElement(name = "testcall")
public class TestCall {

/** The method. */
@XmlElement(required = true)
protected String method;

/** The dateref. */
@XmlElement(required = true)
protected String dateref;

/** The timeref. */
@XmlElement(required = true)
protected String timeref;

/** Getters and Setters of elements 
... */
}    

我需要以xml形式返回響應,如下所示: <testcall> <status>ERROR</status> <msgid>0001</msgid> <msgtxt>INVALID XML DOCUMENT</msgtxt> </testcall>

堆棧應通過向客戶端返回500錯誤來為您處理此問題。 您不需要做任何工作。

編輯:如果您堅持要實現這一點(如果您是我,我會退后一步,除非要求您符合現有接口),您應該添加一個JAX-WS處理程序( javax.xml.ws.handler.Handler )並覆蓋handleFault以檢查無法解組傳入消息時JAX-WS拋出的特定異常。 然后在處理程序中創建響應XML。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM