gpt4 book ai didi

java - 如何修复此错误 :"Attempted to serialize java.lang.Class: org.springframework.beans.factory.annotation.Qualifier"

转载 作者:行者123 更新时间:2023-12-02 06:10:58 26 4
gpt4 key购买 nike

我正在尝试从 Web 服务获取 token ,并且使用 Spring boot 进行编码,但是当我运行该应用程序时,收到以下错误消息:“尝试序列化 java.lang.Class:org.springframework.beans.factory.annotation.Qualifier。忘记注册类型适配器?”。我看过不同的网上帖子都有同样的问题,但我不明白我做错了什么。

我进行了调试,直到出现错误并且 tokenRequest 包含调用的所有信息

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) {

}


}

}

堆栈跟踪

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

最佳答案

这里,TokenRequest是一个Spring-bean,它不是一个简单的java对象,而是一个具有spring特定属性的代理。因此,当您调用 authorizationApi.token(tokenRequest) 时,它会尝试序列化对象并失败,因为它无法序列化特定于 bean 的类(在您的情况下为 Qualifier)。

TokenRequest不应该是一个spring管理的bean,而是一个简单的java对象。因此,删除 Autowiring 并将其设为方法变量实例,而不是保留在类级别。

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

关于java - 如何修复此错误 :"Attempted to serialize java.lang.Class: org.springframework.beans.factory.annotation.Qualifier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55906104/

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