Skip to main content

Posts

Showing posts with the label REST

SpringBoot : Read JSON object from POST Request in Filter

Points To Remember Add the following dependency to the gradle project. compile('com.fasterxml.jackson.core:jackson-databind:2.7.1-1') Read JSON in Filter and bind it to POJO class You can read the JSON inside a Filter class in SpringBoot by  Create a BufferedReader object by reading request object. Create an ObjectMapper object. Read JSON from bufferedReader object and bind it to any POJO class. view plain copy to clipboard print ? package  com.ekiras.test;      import  com.fasterxml.jackson.databind.JsonMappingException;   import  com.fasterxml.jackson.databind.ObjectMapper;      import  javax.servlet.*;   import  java.io.BufferedReader;   import  java.io.IOException;      /**    * @author ekansh    * @since 26/3/16    */    public   class  MyFilter  implements  Filter { ...

SpringBoot : How to do Exception Handling in Rest Application

Points To Remember There are three ways to do exception handling in spring boot application. Global Level using -   @ControllerAdvice Controller Level using -  @ExceptionHandler   Method Level using -  try/catch    Difference between @ExceptionHandler and @ControllerAdvice How to do Exception Handling in Rest Application To demonstrate Exception Handling we will make three Exceptions. BaseException that will be parent of all the Custom Exceptions. CustomException1 that extends Base Exception. CustomException2 that extends Base Exception. We will create the following methods ex1() throws BaseException ex2() throws CustomException1 ex3() throws CustomException2 ex4() throws NullPointerException  ex5() throws NumberFormatException and now we will see how we can handle these exceptions using above mentioned ways. Way 1 : Global Exception Handling using @ControllerAdvice Add a class with annotation @ControllerAdvice and  @RestContro...

RestTemplate : How to get response from a server by HTTP GET call

Points To Remember You need to add the following dependency to your project to use Spring Rest Template. compile('org.springframework:spring-web:3.0.2.RELEASE') How to get response from a server by HTTP GET call Following are the methods that can be used to get the response from a url using HTTP GET method. getForObject() This method takes a URL , Response class  (it tells that the response should be bound to this class). RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject("http://localhost:8080/someUrl",String.class); Following method will make a Http GET call with params. Map<String,String> params = new LinkedHashMap<String, String>(); params.put("userId","1"); RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject("http://localhost:8080/someUrl",String.class,params); This method does not give the following Http status code for the GET cal...

How to Send Rest Response For Lazy fetched Hibernate Objects with Jackson

How to Send Rest Response For Lazy fetched Hibernate Objects with Jackson Just add the following class to your application and your jackson will be configured to send the JSON response for the LAZY fetched domain objects. You need to add the dependency  compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.6.1") @Configuration @EnableWebMvc public class HibernateAwareObjectMapper extends WebMvcConfigurerAdapter { //More configuration.... /* Here we register the Hibernate4Module into an ObjectMapper, then set this custom-configured ObjectMapper * to the MessageConverter and return it to be added to the HttpMessageConverters of our application*/ public MappingJackson2HttpMessageConverter jacksonMessageConverter(){ MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper mapper = new ObjectMapper(); //Registering Hibernate4Module to support lazy objects mapper.r...

Test CAS Rest API from Java Code

Points To Remember Make sure that your CAS Server is up and running. How to set up CAS Rest api with JDBC Authentication. You have created a database and have dummy data in it. Program : Test CAS Rest Api from a Java Code You can use the following piece of code to test the CAS Rest API. You need to follow the following points for Authentication a user on CAS You need to make a GET or POST call depending on your CAS server setup. If the Username and Password are correct then you will get a TGT (Ticket Granting Token) Now we will make a call to the service url of our application to get the Service Ticket. On success you will get a Service Ticket If you have service the Service Token,  then you have successfully authenticated the user. Save this service ticket in a cookie or session, since a service ticket can be used only once You will get the following type of response if everything is working fine. string s = username=admin%40gmail.com&password=igdefault 201 https://ekansh:84...