RestClient抛出NullPointerException?

时间:2014-12-03 00:31:32

标签: java spring rest spring-mvc

我得到了一个N​​ullpointerException,但我甚至不明白它是怎么可能的。我只是从其他类复制粘贴部分RestTemplate restTemplate = this.restClient.getRestTemplate()但是在这里我得到一个NullPointerException?!

@Component
public class ShowRestClient implements ShowService{

@Autowired
private RestClient restClient;
@Override
public List<ShowDto> getShowsByPerformanceID(int perfID)
        throws ServiceException {
    // TODO Auto-generated method stub
    return null;
}
@Override
public List<ShowDto> findAllShows() throws ServiceException {

    RestTemplate restTemplate = this.restClient.getRestTemplate();(!!!!! HERE)
    String url = this.restClient.createServiceUrl("/show/");
    HttpEntity<String> entity = new HttpEntity<String>(this.restClient.getHttpHeaders());
    List<ShowDto> shows = null;
    try {
        ParameterizedTypeReference<List<ShowDto>> ref = new ParameterizedTypeReference<List<ShowDto>>() {};
        ResponseEntity<List<ShowDto>> response = restTemplate.exchange(URI.create(url), HttpMethod.GET, entity, ref);
        shows = response.getBody();
    } catch (RestClientException e) {
        throw new ServiceException("Could not retrieve shows: " + e.getMessage(), e);
    }
return shows;
}

1 个答案:

答案 0 :(得分:1)

您的RestClient似乎最null因为@Autowired无法正常工作。我怀疑你的组件不是一个真正的Spring Bean,因为如果它是,那么你的上下文创建将在app启动期间失败。确保将包中包含在包扫描中。