当前位置: 首页 > 知识库问答 >
问题:

MOXy:抛出实体类型与处理程序返回值不同的WebApplicationException

柳胜
2023-03-14

我有一个使用泽西和MOXy的JAX-RS服务。我有一个处理程序,它返回类型Memo的JSON或XML(取决于接受:标头)表示,但是如果没有找到该项目,它应该返回一个404错误不同的类型MemoError

在第一种情况下(返回Memo),它可以正常工作,但在第二种情况下(抛出带有MemoError实体的WebApplication ationExcture)则不能正常工作。

如果我将处理程序更改为返回一个memorror,则第二种情况确实有效(但第一种情况显然无效)。

它在这两种情况下都可以正确地使用Accept:application/xml(使用默认的JAXB实现),因此我认为问题在于MOXy。

处理程序:

@Path("{id}")
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Memo getMemo(@PathParam("id") long id) {
    Memo memo = memoService.getMemo(id);
    if (memo == null) {
        throw new WebApplicationException(Response
                .status(Response.Status.NOT_FOUND)
                .entity(new MemoError(4, "Not found")).build());
    }
    return memo;
}

MemoError:

@XmlRootElement(name = "error")
public class MemoError {
    private int code;
    private String message;

    public MemoError() {
    }

    public MemoError(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

错误日志

Dec 26, 2013 1:05:55 PM org.glassfish.jersey.server.ServerRuntime$Responder process
SEVERE: Error occurred when processing a response created from an already mapped exception.
Dec 26, 2013 1:05:55 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet JAX-RS Servlet threw exception
Local Exception Stack: 
Exception [EclipseLink-25007] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor for class my.package.MemoError was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.
    at org.eclipse.persistence.exceptions.XMLMarshalException.descriptorNotFoundInProject(XMLMarshalException.java:139)
    at org.eclipse.persistence.internal.oxm.Context$ContextState.getSession(Context.java:143)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.getSession(XMLContext.java:787)
    at org.eclipse.persistence.oxm.XMLContext$XMLContextState.getSession(XMLContext.java:1)
    at org.eclipse.persistence.internal.oxm.Context.getSession(Context.java:451)
    at org.eclipse.persistence.oxm.XMLContext.getSession(XMLContext.java:356)
    at org.eclipse.persistence.oxm.XMLContext.getSession(XMLContext.java:1)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:1119)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:869)
    at org.eclipse.persistence.internal.oxm.XMLMarshaller.marshal(XMLMarshaller.java:848)
    at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:401)
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.writeTo(MOXyJsonProvider.java:808)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:243)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:230)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:574)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:381)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:421)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:265)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

共有1个答案

麻阳
2023-03-14

您可以在Memo类上添加@xmlseealash({memorerror.class})

 类似资料:
  • 来自实体框架后台,我可以将ORM结果强制转换为包含完整后端模型数据子集的类。 我有一个JAX-RS REST服务,在那里我通常返回如下内容 我知道我能做到: 但是,我可以a:将我的结果强制转换为一个单独的类,或者B在我的createquery中命名我的字段,以便在返回我的JSON时命名它们吗? 例如,在.NET领域,我可以做一些类似于.... 然后将其转换为其他类型。我尝试使用typedquery

  • 我的结账代码 在浏览器调试中,我看到一篇文章https://www.paypal.com/smart/api/order/23H57866L56525306/capture 我从贝宝那里得到了答案 我什么时候去https://developer.paypal.com/docs/api/orders/v2/#error-交易被拒绝了,不知道该怎么办。

  • 问题内容: 今天,我偶然发现了一些我什至没想到可以编译的Java代码。减少到最低限度,它看起来像这样: 乍一看,类型参数的的方法和期待,因为没有必要,不使用其他任何地方。无论如何,我发现这在允许冲突的返回值类型共存于同一实现中起着至关重要的作用:如果忽略了一个或两个,则代码不会编译。这里是非工作版本: 我不需要修复上面的代码片段,因为这些只是我用来解释我观点的示例。我只是想知道为什么编译器对它们的

  • 问题内容: 我正在玩Go,发现一个我无法解决的问题。假设我有如下代码: 我导入了软件包并开始使用它: 我真的很喜欢我的助手“运行”,但是我想使其更加慷慨:我不希望人们总是向我传递MySQL客户端。可以是具有“ RunQuery”和“ Result”方法的任何东西。所以我尝试使用接口: 可悲的是,这不再编译了。我收到此错误: Go不支持此功能吗,或者我做错了什么? 问题答案: 应该返回接口,否则你总

  • 问题内容: 我在Utilities类中使用loadImage方法,并且在通过闭包返回图像时遇到了一些麻烦。基本上因为我的代码可能返回图像或错误,所以在调用该方法时将其分配给image属性是行不通的。 我在类的方法声明中使用的方法是否错误,还是应该以不同的方式调用该方法以预期潜在的不同结果?谢谢 问题答案: 将处理程序添加到您的 loadImage 函数中: 迅捷3 像这样调用func: 斯威夫特2

  • 问题内容: 在学习Spring框架时,我在《 Spring in Action 》一书中注意到,作者并未在控制器中使用方法返回类型。作者将控制器方法声明为的返回类型,并且该方法中的return子句仅返回诸如以下的字符串 有人可以详细说明其工作方式的内部差异吗? 问题答案: 这是深入了解。 Spring提供了一个通常处理所有请求的类。它以其方法做到这一点 最终对象在哪里,是用注释的控制器方法的包装。