3

有効なXMLが本文に含まれていることをJAX-RSWebサービスリクエストでチェックインしたいと思います。ただし、このコード:

@PUT
@Produces(MediaType.TEXT_XML)
@Consumes(MediaType.TEXT_XML)
@Path("/{objectID}")
public MyObject updateMyObject(@PathParam("objectID") String existingObjectID, JAXBElement<MyObject> object)
{
    MyObject udpatedObject = null;

    try
    {
        udpatedObject = object.getValue();
    }
    catch (Throwable ex)
    {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);            
    }

    // carry one with processing
}

予想される400エラーではなく、500内部サーバーエラーを返します。例外をキャッチする方法はありますか?

例外スタックトレースの開始は次のとおりです。

Local Exception Stack: 
Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: org.xml.sax.SAXParseException: Premature end of file.
at org.eclipse.persistence.exceptions.XMLMarshalException.unmarshalException(XMLMarshalException.java:92)
4

1 に答える 1

2

例外マッパーを使用できます。例外がスローされると、応答コードに変換されます。

import javax.persistence.NoResultException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class XMLMarshalExceptionMapper implements ExceptionMapper<XMLMarshalException> {

    public Response toResponse(XMLMarshalException exception) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }

}

参照する:

于 2010-08-03T20:40:07.130 に答える