我得到一个NullpointerException但我甚至不明白它怎么可能。我只是从其他类复制粘贴部分RestTemplate restTemplate = this.restClient.getRestTemplate(),但在这里我得到一个NullPointerException?!RestClient抛出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;
}
“2.检查您注入的类ShowRestClient是合适的bean“那! :) – 2014-12-03 11:22:00
这会发生一些时候,人们错误地使用new运算符创建类的对象,并尝试在该类中自动装入或注入依赖。 – Panther 2014-12-04 18:39:56