什么处理转换异常?

时间:2015-10-30 14:39:07

标签: java jackson jax-rs restlet

如果有一个像这样的POST方法的资源

@POST
@Consumes({"application/json"})
@Produces({"application/json"})
public Response createProfileExtensionSchema(ExtensionMetaDataRest extensionSchema, @Context UriInfo info) 

对象ExtensionMetaDataRest使用jackson

注释

如果有JAX-RS应用程序

public class MyJaxrsApplication extends JaxRsApplication implements InitializingBean, BeanFactoryAware {

  @Autowired
  @Qualifier("restletComponent")
  public Component component;

  @Autowired
  public MyJaxrsApplication(final Context context) {
    super(context);
  }

  public void afterPropertiesSet() throws Exception {
    setContext(component.getContext().createChildContext());
    add(new MyApplication());
    getJaxRsRestlet().addClass(ServiceExceptionMapper.class);
    getJaxRsRestlet().addClass(BaseExceptionMapper.class);
    component.getDefaultHost().attach(this);
  }

  public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
    setObjectFactory(new ObjectFactory() {
      public <T> T getInstance(Class<T> jaxRsClass) throws InstantiateException {
        // Bean instantiation is delegated to spring
        return beanFactory.getBean(jaxRsClass);
      }
    });
  }
}

我是这样开始的:

public void start() throws Exception {

  LOGGER.info("Starting web component");

  application.setDebugging(true);

  restletComponent.getDefaultHost().attach(configService.getBaseUrl(), application);
  restletComponent.start();
  //following added to test if status service was used
  final StatusService statusService = application.getStatusService();
  if (!statusService.isEnabled()) {
    statusService.setEnabled(true);
  }
  if (!statusService.isStarted()) {
    statusService.stop();
  }
  proxyManager.configureProxy();

  //Start proxy now
  camelContext.start();
  camelContext.startAllRoutes();
  LOGGER.info("Web component started");

  }

所以我依靠ExceptionMapper来处理响应异常。这在大多数情况下工作正常,除非我有JSON错误。如果json格式不正确,我希望通过异常映射器,但我得到了400 Bad Request,其中包含以下内容:

org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException: Could not convert the message body to a com.genesys.ucs.cs.model.ExtensionMetaDataRest
    at org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException.object(ConvertRepresentationException.java:49)
    at org.restlet.ext.jaxrs.internal.wrappers.params.EntityGetter.getValue(EntityGetter.java:107)
    at org.restlet.ext.jaxrs.internal.wrappers.params.ParameterList.get(ParameterList.java:1123)
    at org.restlet.ext.jaxrs.internal.wrappers.AbstractMethodWrapper.internalInvoke(AbstractMethodWrapper.java:160)
    at org.restlet.ext.jaxrs.internal.wrappers.ResourceMethod.invoke(ResourceMethod.java:283)
    at org.restlet.ext.jaxrs.JaxRsRestlet.invokeMethod(JaxRsRestlet.java:997)
    at org.restlet.ext.jaxrs.JaxRsRestlet.handle(JaxRsRestlet.java:746)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202)
    at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:75)
    at org.restlet.Application.handle(Application.java:385)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.routing.Router.doHandle(Router.java:422)
    at org.restlet.routing.Router.handle(Router.java:639)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.routing.Router.doHandle(Router.java:422)
    at org.restlet.routing.Router.handle(Router.java:639)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202)
    at org.restlet.Component.handle(Component.java:408)
    at org.restlet.Server.handle(Server.java:507)
    at org.restlet.engine.connector.ServerHelper.handle(ServerHelper.java:63)
    at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:143)
    at org.restlet.ext.jetty.JettyServerHelper$WrappedServer.handle(JettyServerHelper.java:273)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
 at [Source: HttpInputOverHTTP@2725f2e5; line: 2, column: 39] (through reference chain: com.genesys.ucs.cs.model.ExtensionMetaDataRest["attributes"]->java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:189)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:249)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:206)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:238)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
    at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1269)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:864)
    at org.restlet.ext.jackson.JacksonRepresentation.getObject(JacksonRepresentation.java:299)
    at org.restlet.ext.jackson.JacksonConverter.toObject(JacksonConverter.java:208)
    at org.restlet.service.ConverterService.toObject(ConverterService.java:229)
    at org.restlet.ext.jaxrs.internal.provider.ConverterProvider.readFrom(ConverterProvider.java:144)
    at org.restlet.ext.jaxrs.internal.wrappers.provider.SingletonProvider.readFrom(SingletonProvider.java:409)
    at org.restlet.ext.jaxrs.internal.wrappers.params.EntityGetter.getValue(EntityGetter.java:101)
    ... 42 more
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
 at [Source: HttpInputOverHTTP@2725f2e5; line: 2, column: 39]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:437)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleOddName(UTF8StreamJsonParser.java:1799)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._parseName(UTF8StreamJsonParser.java:1496)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:693)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:231)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:232)
    ... 56 more

我不明白为什么不能通过异常映射器处理它。

我尝试禁用状态服务,没有任何改变。 我试图添加自己的,但它没有被使用。

如果资源内部发生错误,则会调用异常映射器,但不会在jackson转换期间调用异常映射器。我希望能够在json转换错误的情况下自定义发送回用户的消息

1 个答案:

答案 0 :(得分:0)

在查看JAXRS扩展的代码之后,似乎ConvertRepresentationException被直接包装到JAXRS WebApplicationException中。应该在此级别使用JaxRsProviders.convert方法,但事实并非如此。因此,在这种情况下,您无法利用JAXRS ExceptionHandler。

有关更多详细信息,请参阅JaxRsRestlet#invokeMethodExceptionHandler#convertRepresentationExc方法的内容。似乎JaxRsRestlet#handleInvocationTargetExc方法的内容是您期望的行为。

我为此创建了一个问题:https://github.com/restlet/restlet-framework-java/issues/1162

希望它能帮助你, 亨利