我正在尝试从Web服务获取令牌,并且正在使用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) {
}
}
}
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
答案 0 :(得分:0)
在这里,TokenRequest
是一个Spring Bean,它不是简单的Java对象,而是具有Spring特定属性的代理。因此,当您调用authorizationApi.token(tokenRequest)
时,它尝试序列化该对象并失败,因为它无法序列化特定于bean的类(在您的情况下,Qualifier
)。
TokenRequest不应是Spring管理的bean,而应是简单的Java对象。因此,删除自动装配并使其成为方法变量实例,而不要保持在类级别。
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.setGrantType(TokenRequest.GrantTypeEnum.PASSWORD);
tokenRequest.setUsername(username);
tokenRequest.setPassword(password);
tokenResponse=authorizationApi.token(tokenRequest);
accessToken = tokenResponse.getAccessToken();
答案 1 :(得分:0)
尝试使用以下代码从带有POST请求的Web服务获取令牌。它将起作用。
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");
}
}