本文介绍了Apache Camel 在反序列化对象时抛出异常:com.fasterxml.jackson.core.JsonParseException: Unrecognized token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回字符串的rest api:

I have a rest api that returns a String:

@GetMapping("/api/users")
public String getUsers(){
    return "DENIS";
}

我从 apache camel 调用这个 api:

I'm calling this api from apache camel:

from("direct:start")
    .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
    .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
    .to("http://localhost:8085/api/users")
    .unmarshal().json(JsonLibrary.Jackson, String.class);

val template = DefaultFluentProducerTemplate.on(camelContext);
String a = template.to("direct://" + "start").request(String.class);

这些操作会导致此错误:

These actions result in this error:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'DENIS': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream); line: 1, column: 6]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2337) ~[jackson-core-2.12.1.jar:2.12.1]
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:720) ~[jackson-core-2.12.1.jar:2.12.1]
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3593) ~[jackson-core-2.12.1.jar:2.12.1]

此外,还有一个奇怪的行为:

Moreover, here's another strange behavior:

@Override
public void configure() {
    from("timer://test?period=2000")
        .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
        .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
        .to("http://localhost:8085/api/users")
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) {
                String body = exchange.getIn().getBody(String.class);
            }
        });
    }
}

如果代码是这样的,那么字符串就是反序列化的,但是如果字符串所在的位置是某个对象,那么就一直为null,不会有错误,就是null.虽然在调试器中可以看到对象已经到达,它的字段,但骆驼看不到它.

If the code is like this, then the string is deserialized, but if the string location is some object, it will always be null, there will be no error, it will be null. Although in the debugger it will be visible that the object has arrived, its fields, but camel does not see it.

我已经尝试了很多选择,但我不明白是怎么回事.如果我调用返回布尔值的 api,并且我接受它,那么一切正常,但它不适用于对象和字符串.

I have already tried many options, but I can not understand what is the matter. If I call the api that returns boolean, and I accept it, then everything is ok, but it doesn't work with objects and strings.

我该如何解决这种情况?可能是什么原因?我已经尝试过使用依赖项,但没有结果.

How can I fix the situation? What could be the reason? I've already tried playing with dependencies, but there were no results.

非常感谢您的帮助

推荐答案

第一个案例是这样决定的:

The first case decided so:

from("direct:start")
            .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
            .to("http://localhost:8085/api/users")
            .unmarshal(new JacksonDataFormat(User[].class));

在 JacksonDataFormat 的帮助下.

with the help JacksonDataFormat.

第二个选项无法完全解决,这是发生的事情:

The second option could not be completely solved, here's what happened:

        from("direct:start")
            .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
            .to("http://localhost:8085/api/users")
            .to("direct:httpClient")

            .process(new Processor() {
                         @Override
                         public void process(Exchange exchange) throws JsonProcessingException {
                             String body = exchange.getIn().getBody(String.class);
                             User[] users = jacksonDataFormat.getObjectMapper().readValue(body, User[].class);
                             exchange.getIn().setBody(users);
                         }
            })
            .to("direct:httpClient");

必须先将主体转换为字符串,然后将字符串转换为用户.如果你试图立即将 body 变成 User,它总是为 null,我不明白为什么.

The body must be turned first into a String, and then String into a User.If you try to turn the body immediately into a User, it will always be null, I can't understand why.

问题部分解决,我会在这个网站上再次询问第二部分(Apache Camel:我无法从身体中取出物体并对其进行转换)

The question is partially resolved, I will ask the second part again on this site (Apache Camel: I can't get an object out of the body and transform it)

这篇关于Apache Camel 在反序列化对象时抛出异常:com.fasterxml.jackson.core.JsonParseException: Unrecognized token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:40