gpt4 book ai didi

java - 什么处理了转换异常?

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:17 24 4
gpt4 key购买 nike

如果有一个带有这样的 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 转换错误时发送回用户的消息

最佳答案

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

有关更多详细信息,请参阅 JaxRsReSTLet#invokeMethodExceptionHandler#convertRepresentationExc 方法的内容。看起来 JaxRsReSTLet#handleInitationTargetExc 方法的内容就是您所期望的行为。

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

希望对你有帮助蒂埃里

关于java - 什么处理了转换异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33438562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com