简体   繁体   中英

Quarkus com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class

I'm using quarkus to call a web service, this service needs an object in the header of the request for the authentication:

public class UserInfoDto {

    private String jwtToken;
    private String pgeToken;

    public UserInfoDto(String jwtToken, String pgeToken) {
        this.jwtToken = jwtToken;
        this.pgeToken = pgeToken;
    }

    public String getJwtToken() {
        return jwtToken;
    }

    public void setJwtToken(String jwtToken) {
        this.jwtToken = jwtToken;
    }

    public String getPgeToken() {
        return pgeToken;
    }

    public void setPgeToken(String pgeToken) {
        this.pgeToken = pgeToken;
    }

    @Override
    public String toString() {
        return "UserInfoDto{" +
                "jwtToken='" + jwtToken + '\'' +
                ", pgeToken='" + pgeToken + '\'' +
                '}';
    }
}

Then in the class that generete the header I'm doing this:

@ApplicationScoped
public class RequestUserInfoHeaderFactory implements ClientHeadersFactory {

    @Inject
    PreTokenRetriever preTokenRetriever;

    @Inject
    ObjectMapper mapper;

    private static final String USER_INFO_HEADER = "user-info";

    @Override
    public MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders, MultivaluedMap<String, String> clientOutgoingHeaders) {
        MultivaluedMap<String, String> result = new MultivaluedHashMap<>();
        try {
            OpenIdToken token = preTokenRetriever.getToken();
            UserInfoDto userInfoDto = new UserInfoDto(token.getId_token(), token.getAccess_token());
            System.out.println("Called pre results:" + userInfoDto);
            result.add(USER_INFO_HEADER, mapper.writeValueAsString(userInfoDto));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}

When i start the application with mvn quarkus:dev everything is fine:

__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2022-08-02 17:17:04,000 INFO  [io.quarkus] (Quarkus Main Thread) udp-rmq-pub-unpub-quest-cron 1.0.3-SNAPSHOT on JVM (powered by Quarkus 2.10.3.Final) started in 1.758s. 
2022-08-02 17:17:04,008 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2022-08-02 17:17:04,009 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, rest-client, rest-client-jackson]
Calling procedure
2022-08-02 17:17:04,365 INFO  [OpenIdProvider] (Quarkus Main Thread) OpenId Provider. Contacting endpoint https://***/PRE-EBA-RS/service/SSOToken/generate
2022-08-02 17:17:04,394 INFO  [OpenIdProvider] (Quarkus Main Thread) OpenId Provider. Auth-Scheme=Basic
2022-08-02 17:17:04,397 INFO  [OpenIdProvider] (Quarkus Main Thread) OpenId Provider. Input body {"tokenType":"openId","tokenConsumerURL":null,"attributes":[{"name":"client_id","values":["UDP-001"]},{"name":"response_type","values":["id_token"]}]}
2022-08-02 17:17:04,656 INFO  [OpenIdProvider] (Quarkus Main Thread) OpenId Provider. Response with httpCode=200
Called pre results:UserInfoDto{jwtToken='***', pgeToken='null'}
Done
2022-08-02 17:17:06,117 INFO  [io.quarkus] (Quarkus Main Thread) udp-rmq-pub-unpub-quest-cron stopped in 0.017s

But when I deploy on cloud inside a docker container (quarkus is builded native I don't know if it's important) I got:

2022-08-02 15:12:43,753 ERROR [io.qua.run.Application] (main) Failed to start application (with profile prod): com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class eu.unicredit.regulatoryTool.common.model.UserInfoDto and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:46)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:29)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4568)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3821)
    at eu.unicredit.regulatoryTool.rest.client.RequestUserInfoHeaderFactory.update(RequestUserInfoHeaderFactory.java:32)
    at eu.unicredit.regulatoryTool.rest.client.RequestUserInfoHeaderFactory_ClientProxy.update(Unknown Source)
    at org.jboss.resteasy.microprofile.client.header.ClientHeadersRequestFilter.lambda$filter$4(ClientHeadersRequestFilter.java:61)
    at java.util.Optional.ifPresent(Optional.java:183)
    at org.jboss.resteasy.microprofile.client.header.ClientHeadersRequestFilter.filter(ClientHeadersRequestFilter.java:61)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:767)
    at org.jboss.resteasy.microprofile.client.impl.MpClientInvocation.filterRequest(MpClientInvocation.java:75)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:491)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:152)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:115)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
    at com.sun.proxy.$Proxy221.callProcedure(Unknown Source)
    at java.lang.reflect.Method.invoke(Method.java:566)
    at org.jboss.resteasy.microprofile.client.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:146)
    at com.sun.proxy.$Proxy222.callProcedure(Unknown Source)
    at eu.unicredit.regulatoryTool.rest.client.AdminQuestionnaireResource.callProcedure(AdminQuestionnaireResource.java:21)
    at eu.unicredit.regulatoryTool.rest.client.AdminQuestionnaireResource_ClientProxy.callProcedure(Unknown Source)
    at eu.unicredit.regulatoryTool.CronPubUnpubMain.run(CronPubUnpubMain.java:18)
    at eu.unicredit.regulatoryTool.CronPubUnpubMain_ClientProxy.run(Unknown Source)
    at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:124)
    at io.quarkus.runtime.Quarkus.run(Quarkus.java:67)
    at io.quarkus.runtime.Quarkus.run(Quarkus.java:41)
    at io.quarkus.runner.GeneratedMain.main(Unknown Source)

This is so strange, why I'm getting this error? I don't want to marshall UserInfoDto manually it can grow

Someone can help?

You need to register UserInfoDto for reflection. GraalVM removes all the classes, fields and methods that do not seem to be used by the application. And when using reflection, most of the time, it can't figure out they are used.

Quarkus has some mechanisms to detect things and register some of the classes automatically but in your case, you're pushing your class right to Jackson so Quarkus can't detect it.

Just add the @RegisterForReflection annotation to your UserInfoDto class.

You can find more details in our documentation: https://quarkus.io/guides/writing-native-applications-tips#registering-for-reflection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM