亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復此錯誤:“嘗試序列化 java.lang.Class:

如何修復此錯誤:“嘗試序列化 java.lang.Class:

一只甜甜圈 2022-10-26 16:41:08
我正在嘗試從 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@ComponentScanpublic class FE_ControlController {    @Autowired    private FE_ControlService fe;    @Autowired    private ApiClient api;    @Autowired    private AuthorizationApi authorizationApi;    @Autowired    private TokenRequest tokenRequest    }}
查看完整描述

2 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超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();


查看完整回答
反對 回復 2022-10-26
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

嘗試使用以下代碼通過 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");

    }

}    


查看完整回答
反對 回復 2022-10-26
  • 2 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號