[英]java.lang.NullPointerException on DiscoveryClient when calling the method from another class
When I am calling the method itself through the browser it's working and I am getting the expected JSON
. 当我通过浏览器调用方法本身时,它正在工作,并且得到了预期的
JSON
。 So the method is working and the requested service is registered. 因此,该方法有效,并且请求的服务已注册。
@RequestMapping("/getArticle/{articleId}")
public String getArticle(@PathVariable("articleId") Integer articleId) {
// {"articleId":47,"name":"test","price":5.0}
List<ServiceInstance> instances =
discoveryClient.getInstances("articlemicroservice"); //null when calling
ServiceInstance serviceInstance = instances.get(0);
String baseUrl = serviceInstance.getUri().toString();
baseUrl = baseUrl + "/db/find/" + articleId.toString();
System.out.println("BASEURL: " + baseUrl);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = null;
try {
response = restTemplate.exchange(baseUrl, HttpMethod.GET,
getHeaders(), String.class);
} catch (Exception ex) {
System.out.println(ex);
}
// {"articleId":47,"name":"test","price":5.0}
String articleEntity = response.getBody().toString();
System.out.println("articleEntity: " + articleEntity);
return articleEntity;
}
When I am calling the method from another class (which is located in the same project) I am getting a NullPointerException
. 当我从另一个类(位于同一项目中)调用该方法时,我得到了
NullPointerException
。
@GetMapping(path = "/addCartItemToCart/{cartId}/{articleId}/{quantity}")
public @ResponseBody String addCartItemToCart(@PathVariable("cartId")
Integer cartId,
@PathVariable("articleId") Integer articleId,
@PathVariable("quantity") Integer quantity) {
ArticleMicroserviceRestConnectorRequester n = new
ArticleMicroserviceRestConnectorRequester();
String article = n.getArticle(articleId); //calling the method here
JSONObject json = new JSONObject(article);
CartItemEntity cartItemEntity = new CartItemEntity();
cartItemEntity.setArticleId(json.getInt("articleId"));
cartItemEntity.setQuantity(quantity);
cartItemEntity.setCartItemId(cartId);
CartEntity cartEntity = cartRepository.findById(cartId).get();
cartEntity.addCartItem(cartItemEntity);
cartEntity.setNumberOfCartItems(cartEntity.getNumberOfCartItems() +
cartItemEntity.getQuantity());
cartRepository.save(cartEntity);
return "Saved";
}
Exception: 例外:
java.lang.NullPointerException: null at de.leuphana.jee.connector.jpa.behaviour.ArticleMicroserviceRestConnectorRequester.getArticle(ArticleMicroserviceRestConnectorRequester.java:44) ~[classes/:na] ...
In addCartItemToCart
you create a new instance of ArticleMicroserviceRestConnectorRequester
. 在
addCartItemToCart
您创建的新实例ArticleMicroserviceRestConnectorRequester
。 It is not managed by Spring so discoveryClient
is not autowired, it is null. 它不是由Spring管理的,因此
discoveryClient
没有自动连接,它为null。
You have to inject ArticleMicroserviceRestConnectorRequester
into your other class. 您必须将
ArticleMicroserviceRestConnectorRequester
注入其他类。 Thus you will get an instance which is managed by Spring and should have autowired/injected discoveryClient
. 因此,您将获得一个由Spring管理的实例,该实例应具有自动接线/注入的
discoveryClient
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.