如何修复此错误:“尝试序列化 java.lang.Class:

我正在尝试从 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


    }


}


一只甜甜圈
浏览 108回答 2
2回答

撒科打诨

这里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();

子衿沉夜

尝试使用以下代码通过 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");    }}    
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java