简体   繁体   English

如何在Spring Boot中自动连接OkHttpClient bean?

[英]How to Autowire a OkHttpClient bean in Spring Boot?

I would like to autowire an instance of OkHttpClient in my Controller class. 我想在Controller类中autowire OkHttpClient的实例。 I have created a OkHttpClientFactory class and marked it as Bean in its constructor. 我创建了一个OkHttpClientFactory类,并在其构造函数中将其标记为Bean I am including this as a Autowired in Controller class. 我将其作为Autowired在Controller类中。 However, I run into the following issue - 但是,我遇到了以下问题-

Bean - 豆角,扁豆 -

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import okhttp3.OkHttpClient;

@Configuration

public class OkHttpClientFactory {
    @Bean
    public OkHttpClient OkHttpClientFactory() {
        return new OkHttpClient();
    }


}

Controller - 控制器-

import java.io.IOException;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sap.lmc.beans.OkHttpClientFactory;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@RestController
@RequestMapping("/recast")
public class Test {

    @Autowired
    private OkHttpClientFactory client;

    private String url = "https://example.com/";

    @GetMapping(path="/fetchResponse", produces="application/json")
    public String getRecastReponse() {

        Request request = new Request.Builder().url(url).build();
        try {
            Response response = client.newCall(request).execute();
            JSONObject json = new JSONObject();
            json.put("conversation",response.body().string());
            return json.toString();
        } catch (IOException e) {
            return e.getMessage().toString();
        }   
    }

}

The following error results - 出现以下错误结果-

java.lang.Error: Unresolved compilation problem: 
    The method newCall(Request) is undefined for the type OkHttpClientFactory

Isn't the Autowired OkHttpClientFactory instance actually returning an object of OkHttpClient type. Autowired OkHttpClientFactory实例实际上不是返回OkHttpClient类型的对象OkHttpClient Then why is the method newCall() not applicable on it ? 那么为什么newCall()方法不适用呢?

Change this @Autowired private OkHttpClientFactory client; 更改此@Autowired private OkHttpClientFactory client; in your controller. 在您的控制器中。

to

@Autowired private OkHttpClient client;

You want to @autowire to OKHttpClient and not the 'Factory' class. 您想要@autowire到OKHttpClient而不是'Factory'类。

Your factory is a configuration class because you annotated it with @Configuration annotation. 您的工厂是配置类,因为您使用@Configuration注释对其进行了注释。 In your controller do not inject the configuration bean, but the bean that is configured inside it. 在您的控制器中,不要注入配置Bean,而是注入其中配置的Bean。 This bean will be available in spring context and valid for @Autowire . 该bean将在spring上下文中可用,并且对@Autowire有效。

The class OkHttpClientFactory has no method newCall(Request) as you can obviously see. OkHttpClientFactory没有法newCall(Request) ,你可以很明显的看出。 You should change the field private OkHttpClientFactory client; 您应该更改字段private OkHttpClientFactory client; in your controller to private OkHttpClient client; 在您的控制器中,以private OkHttpClient client; and let spring inject the bean by type. 然后让spring按类型注入bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM