Friday, 15 February 2019

How to handle error messages from HTTP response using Spring Boot Rest Template.


When a Rest endpoint is called using RestTemplate ,RestTemplate internals will consume the response and it will throw one of the below exception to the calling code based on the response status code it receives .

Below are the two basic exception classes thrown by RestTemplate.

HttpServerErrorException
HttpClientErrorException

Example :

        ResponseEntity responseEntity = restTemplate.postForEntity("/profile/create", entity, String.class);


But how does it know when to create an exception of the above type.

Below are the various scenarios from controller methods.

Case 1 :

Controller received the input. Processed the input .

Server method throws an exception (something like throw new Exception(),throw new  RuntimeException())
 
if(request.getId()==null){
throw WrongDataException("I think you sent wrong data");
 }

 This can be caught in the caller code using HttpServerErrorException .

Remember:  When you throw an exception directly,the HTTP status response sent to client is 500,unless otherwise the exception class being thrown has @ResponseStatus marked like below

 Example:

throw new WrongDataException("I think you sent wrong data");
        @ResponseStatus(value=HttpStatus.NOT_FOUND)
  public class WrongDataException(){
}


Case 2 :

Controller received the input. Processed the input .

if(request.getId()!=someValue){
     return new ResponseEntity("I think you sent wrong data", HttpStatus.NOT_FOUND);
}

still RestTemplate create an exception of type :  HttpClientErrorException .

So Basically

5XX - > HttpServerErrorException
4XX - > HttpClientErrorException

However you can catch all of the above with the base Exception class,but may not able to extract the message you embed inside the ResponseEntity. The message you set in ResponseEntity or in Exception constructor can be consumed only throw below way

catch(HttpServerErrorException e){
    LOGGER.error("error with StatusCode {} , Message {} ",
                                                                   e.getStatusCode(),
                                                                   e.getResponseBodyAsString());
}

If you ever want to get the response message body then you must catch these exception types.

NOTE : RestTemplate does not create any exception if the response status code is 2XX.


Let me know for any feedback/inputs.

Friday, 9 February 2018

Java Monitoring Tools - JConsole Secure Connection Failed

Java is a powerful programming language . Today millions of devices run in Java.Everyday millions of developers write java code and every company writes and compiles thousands of code in java for their service clients.

For earlier java developer ,during the course of development they have to deal with the design and the implementation of java techniques which sometimes looks easy but hard to understand the internals of java mechanisms. Java jdk comes with set of monitoring tools that is shipped inside jdk/bin folder e.g jconsole,jvisualvm . Using them needs a bit of understanding and how to utilize those tools. When you have your java program running , u can start jconsole or jvisualvm.  You can select specific java application and start monitoring. However jconsole cannot launch the application and the below error is shown.











Most of the solution available in internet does not fully solve the purpose. Below are the set of configurations needed which will make the monitoring tools work properly.

Step 1 : Write Java program using any IDE of your like and build a jar out of it .

Step 2: start the jar with the below command

java  -Dcom.sun.management.jmxremote.port=9595
         -Dcom.sun.management.jmxremote.ssl=false
         -Dcom.sun.management.jmxremote.authenticate=false
         -Djava.rmi.server.hostname=localhost  -jar  TestProject.jar

Make sure your java programming runs for sometime until the monitoring tool starts the applications. The monitoring tools does take 2 to 3 mins to find all the loaded applications .

Step 3: Go to JDK/bin  installed path and issue the below command

jconsole localhost:9595

After jconsole is opened,just click Insecure connection


jconsole view of the running programs,its memory usage.





















JvisualVM

At this stage, we can also use jvisualvm. Go to JDK/bin path and start jvisualvm.exe. It list all the java loaded applications and clicking on the selected application will display the Thread/Memory/CPU view.


















Note :

All these monitoring tools will properly work when we run the java program with set of java arguments supplied to the program as mentioned in the Step 2.