0

When I am calling the method itself through the browser it's working and I am getting the expected 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.

    @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] ...

Cœur
  • 37,241
  • 25
  • 195
  • 267
elp
  • 840
  • 3
  • 12
  • 36
  • Apparently `discoveryClient` is `null`. How is it initialized/set? – lexicore Mar 21 '18 at 14:08
  • `@Autowired private DiscoveryClient discoveryClient;` Maybe it's not initialized when calling the method from another class but it's initialized when calling the method itself? But if I dont get the reason. – elp Mar 21 '18 at 14:09
  • 2
    Wait a second. In `addCartItemToCart` you create a new instance of `ArticleMicroserviceRestConnectorRequester`, right? It is not managed by Spring so `discoveryClient` is not autowired. So of course it is `null`. You have to inject `ArticleMicroserviceRestConnectorRequester` into your other class so that it is managed by Spring. – lexicore Mar 21 '18 at 14:16
  • Yep, forgot to use `@Autowired`. I have no idea why I had the idea to initialize it another way. Thank you guys! – elp Mar 21 '18 at 14:21

1 Answers1

2

In addCartItemToCart you create a new instance of ArticleMicroserviceRestConnectorRequester. It is not managed by Spring so discoveryClient is not autowired, it is null.

You have to inject ArticleMicroserviceRestConnectorRequester into your other class. Thus you will get an instance which is managed by Spring and should have autowired/injected discoveryClient.

lexicore
  • 42,748
  • 17
  • 132
  • 221