简体   繁体   English

使用JAXB的jersey webservices中的xml语法无效

[英]invalid xml syntax in jersey webservices using JAXB

I am using jersey web services which consumes POST request as an xml and produces response as an xml. 我正在使用jersey Web服务,该服务使用POST请求作为xml并产生响应作为xml。 I am using JAXB to marshal/Unmarshal xml into Java bean. 我正在使用JAXB将XML编组/解组为Java bean。 Here "TestCall" is a class which stores all the attributes specified in request,but in case XML is malformed(ie end tag missing) in request, i need to return ERROR code in response XML. 这里的“ TestCall”是一个存储请求中指定的所有属性的类,但是如果请求中XML格式不正确(即缺少结束标记),我需要在响应XML中返回错误代码。 so how to handle these scenario ? 那么如何处理这些情况呢?

Here is my code: 这是我的代码:

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

I am marshal the xml using below 2 classes: 我使用以下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();
}

} }

and

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 
... */
}    

I need to return response in xml like below: <testcall> <status>ERROR</status> <msgid>0001</msgid> <msgtxt>INVALID XML DOCUMENT</msgtxt> </testcall> 我需要以xml形式返回响应,如下所示: <testcall> <status>ERROR</status> <msgid>0001</msgid> <msgtxt>INVALID XML DOCUMENT</msgtxt> </testcall>

The stack should handle this for you by returning a 500 error to the client. 堆栈应通过向客户端返回500错误来为您处理此问题。 You don't need to do any work. 您不需要做任何工作。

Edit: if you insist on implementing this (I would push back if I were you, unless you're required to be compliant to an existing interface), you should add a JAX-WS handler ( javax.xml.ws.handler.Handler ) and override handleFault to check for the specific exception that JAX-WS throws when it can't unmarshal the incoming message. 编辑:如果您坚持要实现这一点(如果您是我,我会退后一步,除非要求您符合现有接口),您应该添加一个JAX-WS处理程序( javax.xml.ws.handler.Handler )并覆盖handleFault以检查无法解组传入消息时JAX-WS抛出的特定异常。 Then create the response XML in the handler. 然后在处理程序中创建响应XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM