Skip to main content

Filtering for your restful webServices

Filters are one of the important features provided by the JAX-RS framework. It is used in various contexts. It may be applied on either request to a resource or the response from a resource, or both.Consider a scenario in which we do not want to show some class members in the response. This process is called filtering. Jackson has two annotations that are used in filtering are: @JsonIgnore and @JsonIgnoreProperties.



@JsonIgnore is used at field level to mark a property or list of properties to be ignored.


import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonSampleTest {
   public static void main(String args[]){
      ObjectMapper mapper = new ObjectMapper();
      try{
         Student student = new Student(1,11,"LABORATORY","SAMUEL");       
         String jsonString = mapper
            .writerWithDefaultPrettyPrinter()
            .writeValueAsString(student);
         System.out.println(jsonString);
      }
      catch (IOException e) { 
         e.printStackTrace();
      }     
   }
}
class Student { 
   public int id;
   @JsonIgnore
   public String systemId;
   public int rollNo;
   public String name;

   Student(int id, int rollNo, String systemId, String name){
      this.id = id;
      this.systemId = systemId;
      this.rollNo = rollNo;
      this.name = name;
   }
}
{
   "id" : 1,
   "rollNo" : 11,
   "name" : "SAMUEL"
}

@JsonIgnore  is a member or method level annotation. It expects that the properties to be excluded are marked one by one. If we want to eliminate a member from the process of serialization and deserialization, we can annotate the actual property or its setter or getter. 
@JsonIgnoreProperties is a class-level annotation. It ignores the logical properties in JSON serialization and deserialization.

Comments

Popular posts from this blog

Difference between String ,StringBuffer & StringBuilder?

Run this simple java program and create scenerio of String object creation in your mind.You will see huge difference than expectation.                                I would recommend to first try on your own then follow the solution . As you execute the above line of codes in your Java Runtime Environment .As compiler read the lines .In  case of                                      String a= new String("Hello");        As it will allow compiler to create a new string object .It is always mandatory for  compiler to allocate a memory space in heap whenever new  keyword is used.So String class object created successfully in heap with reference variable a.And a very important point  need to be noted down that here  "new String("Hello');"  ...

AngularJs/javascript and Angular/typescript (version 2/4/latest) have a series of difference.

                         AngularJs vs Angular (Typescript)                                                 On basis of process of compilation          ANGULARJS : Angular Js  compiler  traverses the DOM looking for attributes . It takes two phase for compilation where initial phase  traverse the DOM and collect all of the directives. The result is a linking function.   Next  phase evolves combine the directives with a scope and produce a live view. Any changes in the scope model are reflected in the view, and any user interactions with the view are reflected in the scope model. This makes the scope model the single source of truth.

Design Discussion over Microservices Adaption

 Terms that are most commonly used in any microservices architecture Don't worry after watching so many new terms as below : Saga Pattern Bounded context Eventual consistency Event Command Execution  Event broker CDC (change data capture) event managment Domain driven design Event driven design CQRS (Command query responsibility segregation) Rollback operation  Asynchronous Callback operation Fault tolerance :Bulk Head & Circuit breaker Open API configurations Centralized logging ELK Stats aggregation Aggregates in Domain Driven Design FAT Domain VS Anemic Domain Ubiquitous Language  Event sourcing Event replay Scatter-Gather Pattern Saga Pattern:      A pattern which is the heart soul of any microservice architecture ,it is used to manage data consistency across multiple microservices in distributed transaction .It performs a sequence of local transactions which perform updation operation in services & publish a message or trigger the event. T...