如何通过application.properties使用REST服务获取汇率JSON信息?

huangapple go评论75阅读模式
英文:

How to get exchange rate json information using by application.properties with REST service?

问题

我想从 'https://dovizkurlari-l6vtviaacq-uc.a.run.app/api/doviz/usd' 获取 JSON 数据,并通过一个服务来展示它,但我遇到了一个无法解决的错误。

我在 application.properties 文件中添加了以下属性:

application.properties

tcmb.request.dolar=https://dovizkurlari-l6vtviaacq-uc.a.run.app/api/doviz/usd

然后我在 tcmbCommon 类中将这个链接赋值给变量 dolarUrl

tcmbCommon.java

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TcmbCommon {

    @Value("${tcmb.request.dolar}")
    public String dolarUrl;

}

在一个服务类中,我使用 restTemplate 获取了这个服务的值。

DollarService.java

@Service
@RequiredArgsConstructor
@PropertySource("classpath:application.properties")
public class DolarService {

    @Autowired
    TcmbCommon tcmbCommon;

    public String getDolar(){

        RestTemplate rest = new RestTemplate();
        String data = rest.getForObject(tcmbCommon.getDolarUrl(), String.class);

        return data;
    }
}

最后,我通过在 DolarController 类中调用 DolarService 类来创建一个响应服务。

DolarController.java

@RestController
@RequestMapping(value = "/api/dolar")
@RequiredArgsConstructor
@Log4j2
public class DolarController {

    private @NonNull final DolarService dolarService;

    @GetMapping("/getDolar")
    public ResponseEntity<?> getDolar(){
        return new ResponseEntity<>(dolarService.getDolar(), HttpStatus.OK);
    }
}

当启动项目时,我遇到了这个错误。

***************************
应用启动失败
***************************

描述:

在 com.tcmb.webservices.tcmbwebservices.controller.DolarController 中的构造函数的参数 0 需要一个类型为 'com.tcmb.webservices.tcmbwebservices.services.DolarService' 的 bean,但找不到该类型的 bean。


操作:

考虑在配置中定义一个类型为 'com.tcmb.webservices.tcmbwebservices.services.DolarService' 的 bean。
英文:

I want to take the json data in the 'https://dovizkurlari-l6vtviaacq-uc.a.run.app/api/doviz/usd' and present it with a service. but I got an error I can't solve.

I added the below property in my application.properties

application.properties

tcmb.request.dolar=https://dovizkurlari-l6vtviaacq-uc.a.run.app/api/doviz/usd

Then I assign this link to the variable dolarUrl in tcmbCommon class.

tcmbCommon.java

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TcmbCommon {

    @Value(&quot;${tcmb.request.dolar}&quot;)
    public String dolarUrl;

    }

and in a service class, I got the values for this service with restTemplate

DollarService.java

@Service
@RequiredArgsConstructor
@PropertySource(&quot;classpath:application.properties&quot;)
public class DolarService {

    @Autowired
    TcmbCommon tcmbCommon;

    public String getDolar(){

        RestTemplate rest = new RestTemplate();
        String data = rest.getForObject(tcmbCommon.getDolarUrl(), String.class);

        return data;
    }
}

finally I created an response service by calling the DolarService class in the DolarController class

DolarController.java

@RestController
@RequestMapping(value = &quot;/api/dolar&quot;)
@RequiredArgsConstructor
@Log4j2
public class DolarController {

    private @NonNull
    final DolarService dolarService;

    @GetMapping(&quot;/getDolar&quot;)
    public ResponseEntity&lt;?&gt; sDolar(){
        return new ResponseEntity&lt;&gt;(dolarService.getDolar(), HttpStatus.OK);
    }
}

I get this error when starting the project.

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.tcmb.webservices.tcmbwebservices.controller.DolarController required a bean of type &#39;com.tcmb.webservices.tcmbwebservices.services.DolarService&#39; that could not be found.


Action:

Consider defining a bean of type &#39;com.tcmb.webservices.tcmbwebservices.services.DolarService&#39; in your configuration.

Disconnected from the target VM, address: &#39;127.0.0.1:63388&#39;, transport: &#39;socket&#39;

答案1

得分: 1

你错过了在控制器类中写 @Autowired。

**@Autowired
final DolarService dolarService;**

然后尝试运行。

英文:

you missed to write @Autowired in controller class.

**@Autowired
final DolarService dolarService;**

then try to run.

答案2

得分: 1

请在注入依赖项到DollarService bean时,修改DolarController.java文件如下所示。我猜想在字段上既使用@Autowired又使用final关键字是矛盾的。

@Autowired       // 这将注入DollarService类的依赖项(自动装配)
private DolarService dolarService;

如果您想要在字段上使用final关键字进行自动装配,您需要按照以下方式进行构造函数注入。

@Autowired
public DolarController(DolarService dolarService) {
    this.dolarService = dolarService;
}

请告诉我是否问题已解决。

英文:

Please modify DolarController.java with below code while injecting dependencies to DollarService bean. I guess having @Autowired and final on a field are contradictory.

@Autowired       // this will inject the dependency of DollarService class (autowiring)
private DolarService dolarService;

If you want to use the final keyword autowired, you need to do constructor injection as mentioned below.

@Autowired
public DolarController(DolarService dolarService) {
    this.dolarService = dolarService;
}

Please let me know if it is still not resolved.

huangapple
  • 本文由 发表于 2020年9月20日 17:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63977663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定