简体   繁体   中英

How to fix this error:“Attempted to serialize java.lang.Class: org.springframework.beans.factory.annotation.Qualifier”

I'm trying to obtain a token from a web service, and I'm coding with Spring boot, but when I run the application, I get this error message : "Attempted to serialize java.lang.Class: org.springframework.beans.factory.annotation.Qualifier. Forgot to register a type adapter?". I've looked atdifferent online posts with the same problem, but I don't understand what I did wrong.

I debugged until the error and the tokenRequest contains all information for the calling

package com.ids.app;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.ids.app.controller.FE_ControlController;




@SpringBootApplication(scanBasePackages={"io.swagger.client","com.ids.app.controller","com.ids.app.service"})


public class IdsFeApplication implements CommandLineRunner{



    @Autowired
    private FE_ControlController fec;



    public static void main(String[] args) {
        SpringApplication.run(IdsFeApplication.class, args);        

    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Hello world!!!");



    fec.selWebServiceAndUsernameAndPassword("A");


    }

}


package com.ids.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;

import com.ids.app.entities.FE_Control;
import com.ids.app.service.FE_ControlService;

import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.api.AuthorizationApi;
import io.swagger.client.model.TokenRequest;
import io.swagger.client.model.TokenResponse;

@Controller
@ComponentScan

public class FE_ControlController {

    @Autowired
    private FE_ControlService fe;

    @Autowired
    private ApiClient api;

    @Autowired
    private AuthorizationApi authorizationApi;

    @Autowired
    private TokenRequest tokenRequest;

    @Autowired
    private TokenResponse tokenResponse;

    private String accessToken;



    public void selWebServiceAndUsernameAndPassword(String code) {

        String webService=fe.selWebService(code);
        String username=fe.selUsername(code);
        String password=fe.selPassword(code);



        api.setBasePath(webService+"OAuth");
        authorizationApi.setApiClient(api);
        tokenRequest.setClientId(null);
        tokenRequest.setClientSecret(null);
        tokenRequest.setGrantType(TokenRequest.GrantTypeEnum.PASSWORD);
        tokenRequest.setUsername(username);
        tokenRequest.setPassword(password);
        try {

            System.out.println(tokenRequest.getUsername());
            System.out.println(tokenRequest.getPassword());
            tokenResponse=authorizationApi.token(tokenRequest);
            accessToken=tokenResponse.getAccessToken();
            System.out.println(accessToken);

        } catch (ApiException e) {

        }


    }

}

StackTrace

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at com.ids.app.IdsFeApplication.main(IdsFeApplication.java:28) [classes/:na]
Caused by: java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.springframework.beans.factory.annotation.Qualifier. Forgot to register a type adapter?
    at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:73) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:69) ~[gson-2.8.1.jar:na]
    at com.google.gson.TypeAdapter$1.write(TypeAdapter.java:191) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) ~[gson-2.8.1.jar:na]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) ~[gson-2.8.1.jar:na]
    at com.google.gson.Gson.toJson(Gson.java:669) ~[gson-2.8.1.jar:na]
    at com.google.gson.Gson.toJson(Gson.java:648) ~[gson-2.8.1.jar:na]
    at com.google.gson.Gson.toJson(Gson.java:603) ~[gson-2.8.1.jar:na]
    at com.google.gson.Gson.toJson(Gson.java:583) ~[gson-2.8.1.jar:na]
    at io.swagger.client.JSON.serialize(JSON.java:120) ~[classes/:na]
    at io.swagger.client.ApiClient.serialize(ApiClient.java:747) ~[classes/:na]
    at io.swagger.client.ApiClient.buildRequest(ApiClient.java:998) ~[classes/:na]
    at io.swagger.client.ApiClient.buildCall(ApiClient.java:949) ~[classes/:na]
    at io.swagger.client.api.AuthorizationApi.tokenCall(AuthorizationApi.java:248) ~[classes/:na]
    at io.swagger.client.api.AuthorizationApi.tokenValidateBeforeCall(AuthorizationApi.java:260) ~[classes/:na]
    at io.swagger.client.api.AuthorizationApi.tokenWithHttpInfo(AuthorizationApi.java:285) ~[classes/:na]
    at io.swagger.client.api.AuthorizationApi.token(AuthorizationApi.java:273) ~[classes/:na]
    at com.ids.app.controller.FE_ControlController.selWebServiceAndUsernameAndPassword(FE_ControlController.java:79) ~[classes/:na]
    at com.ids.app.IdsFeApplication.run(IdsFeApplication.java:39) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    ... 5 common frames omitted

Here, TokenRequest is a Spring-bean, which is not a simple java object but a proxy with spring-specific attributes. So, when you invoke authorizationApi.token(tokenRequest) it tries to serialize the object and fails since it's not able to serialize bean-specific classes (in your case Qualifier ).

TokenRequest should not be a spring-managed bean, but a simple java object. So, remove the autowiring and make it a method variable instance instead of keeping at class level.

    TokenRequest tokenRequest = new TokenRequest();
    tokenRequest.setGrantType(TokenRequest.GrantTypeEnum.PASSWORD);
    tokenRequest.setUsername(username);
    tokenRequest.setPassword(password);
    tokenResponse=authorizationApi.token(tokenRequest);
    accessToken = tokenResponse.getAccessToken();

Try with the following code to get token from web service with POST request. It will work.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public void getHttpCon() throws Exception {

    String POST_PARAMS = "grant_type=password&username=someusrname&password=somepswd&scope=profile";
    URL obj = new URL("http://someIP/oauth/token");
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json;odata=verbose");
    con.setRequestProperty("Authorization",
            "Basic Base64_encoded_clientId:clientSecret");
    con.setRequestProperty("Accept",
            "application/x-www-form-urlencoded");

    // For POST only - START
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(POST_PARAMS.getBytes());
    os.flush();
    os.close();
    // For POST only - END

    int responseCode = con.getResponseCode();
    System.out.println("POST Response Code :: " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("POST request not worked");
    }
}    

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