在Spring Boot中配置构建路径

时间:2018-11-08 12:19:08

标签: java spring-boot resttemplate

当我将鼠标指针放在Reservation reservation=restTemplate.getForObject("localhost:8082/flight_reservation/reservation/"+id, Reservation.class);上时 行,然后告诉配置构建路径。

package com.bulbul.flightcheekIn.integration;

import org.springframework.web.client.RestTemplate;
import com.bulbul.flightcheckIn.integration.dto.Reservation;
import com.bulbul.flightcheckIn.integration.dto.ReservationUpdateRequest;

public class ReservationRestClientImpl implements ReservationRestClient {

    @Override
    public Reservation findReservation(Long id) {
        RestTemplate restTemplate = new RestTemplate();
        Reservation reservation = restTemplate.getForObject("localhost:8082/flight_reservation/reservation/"+id, Reservation.class);
        return null;
    }

    @Override
    public Reservation updateReservation(ReservationUpdateRequest request) {
        // TODO Auto-generated method stub
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

在Spring中编写一个使用构建器模式的配置类,如下所示。

@配置 公共类RestTemplateConfig {

.....

@Bean
public RestTemplateBuilder restTemplateBuilder() {
    return new RestTemplateBuilder()
            .rootUri("http://localhost:8082/flight_reservation/reservation/");
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
}

}

您的客户端类自动装配其余模板

公共类ReservationRestClientImpl实现ReservationRestClient {

@Autowired
private RestTemplate restTemplate;

@Override
public Reservation findReservation(Long id) {
    RestTemplate restTemplate = new RestTemplate();
    Reservation reservation = restTemplate.getForObject(""+id, Reservation.class);
    return null;
}

}