Prabhu Sunderaraman

Fullstack Engineer, Programmer, Writer, Trainer

prabhu.bits@gmail.com,     GitHub     Youtube     LinkedIn
  • Home
  • Profile
  • Books
  • Reading List
Browsing: / Home
Shortlink

Short Video on @RequestBody in Spring Boot

By Prabhu Sunderaraman on November 9, 2017 in Spring, Video, Video Sessions
Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

Short Video on Accessing a RESTful service using RestTemplate

By Prabhu Sunderaraman on November 9, 2017 in Spring, Video, Video Sessions
Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

How to create a custom MessageConverter in Spring Boot – II

By Prabhu Sunderaraman on October 31, 2017 in Micro services, Spring

In the first part we discussed the need to convert the Person instance into a comma separated format. By default Jackson throws the JSON formatted response. Let’s create a custom formatter class that will convert the Person object into comma separated format.

Creating a custom message converter is easy. We need to create the class that extends AbstractHttpMessageConverter and override it’s methods. The method that’s our concern is the writeInternal() method. A custom message converter is shown below.

public class CustomMessageConverter extends AbstractHttpMessageConverter<Person> {

    @Override
    protected boolean supports(Class<?> aClass) {
        if(Person.class.equals(aClass))
            return true;
        return false;
    }

    @Override
    protected Person readInternal(Class<? extends Person> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
        return null;
    }

    public static final MediaType MEDIA_TYPE = new MediaType("text", "plain", Charset.forName("utf-8"));

    public CustomMessageConverter() {
        super(MEDIA_TYPE);
    }

    @Override
    protected void writeInternal(Person person, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
        String output = "";
        output += person.getId() + "," + person.getName() + "," + person.getAge();
        httpOutputMessage.getHeaders().setContentType(MEDIA_TYPE);
        final OutputStream out = httpOutputMessage.getBody();

        out.write(output.getBytes(Charset.forName("utf-8")));
        out.close();
    }
}

As you notice, the writeInternal() method does the required. We have generated the comma separated format and written it to the OutputStream. Now, we need to register this converter with the DispatcherServlet like this.

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
   @Override
   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
       super.configureMessageConverters(converters);
       converters.add(new CustomMessageConverter());
   }
}

And that’s it!!!. Send a request to the SampleController and you get the required output :)

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

4 minute video on Hello Spring Boot

By Prabhu Sunderaraman on October 26, 2017 in Micro services, Spring

A number of times I have noticed and followed a particular behaviour while watching technical videos. I usually mute the sound and just watch the video. I have realized that you need to speak out and explain only when you’re talking about programming languages. You can just listen to some mild music and watch someone code in the framework or technology that you want to learn. So I have uploaded a four minute video on Hello in Spring Boot using IntelliJ IDEA. Decided to play some nice instrumental in the background.
It didn’t take much time at all, as expected. I was able to create, edit and upload in less than an hour and this motivates me to upload more such short videos in future. Here is the video.

Share this on: Mixx Delicious Digg Facebook Twitter
Shortlink

How to Create a custom MessageConverter in SpringBoot – I

By Prabhu Sunderaraman on October 25, 2017 in Spring

You have a Spring Boot service that sends out an Object as response. The Object is automatically converted to JSON response, thanks to Jackson parser in the classpath. Let’s look at the service below. It’s a simple service that accepts an id of a person as argument, and returns a person instance.

@RestController
public class SampleController {
    @RequestMapping(value="/{id}")
    public Person getPerson(@PathVariable("id") long id){
        //Look up the DB and fetch the person
        //Let's hardcode that for now
        Person person = new Person();
        person.setId(id);
        person.setName("John");
        person.setAge(40);
        return person;
    }
}

The Person class is a simple POJO with id, name and age properties as shown below.

public class Person {
    private String name;
    private int age;
    private long id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

When you run this as http://localhost:8080/101, Jackson converts the Person instance to JSON and gives you this output.

{"name":"John","age":40,"id":101}

But say, you want to convert it to CSV format or JSON format with some other parser like GSON, here’s where custom converters come into picture. Let’s try to convert Person instance to this format.

101,John,40

We’ll now create a custom HttpMessageConverter and plug it in. And that involves creating a couple of classes.

Share this on: Mixx Delicious Digg Facebook Twitter
« Previous 1 … 19 20 21 … 64 Next »

Youtube Channel




Categories

  • JavaScript (48)
    • RequireJS (5)
  • Go (44)
  • Golang (44)
  • Ext JS (23)
  • Spring (22)
  • Mobile (21)
  • Scala (20)
    • Play (3)
  • Uncategorized (19)
  • Video Sessions (18)
  • GoG (17)
  • Sencha Touch (16)
  • jQuery (14)
  • Languages (13)
  • Java 8 (12)
  • React JS (11)
  • Kotlin (11)
  • HealthyCodeMagazine (9)
  • Video (9)
  • Objective-C (8)
  • NoSQL (8)
  • Android (7)
  • MongoDB (7)
  • GWT (6)
  • Tools (6)
  • HTML 5 (5)
  • Cloud (5)
  • General (5)
  • Micro services (5)
  • Java (5)
  • Books (4)
  • AWS (4)
  • Software Architecture (4)
  • .NET (3)
  • Elixir (3)
  • Docker (3)
  • Reactive (3)
  • NodeJS (2)
  • RoR (2)
  • Backbone (1)
  • AngularJS (1)

Archives

  • 2020 (49)
  • 2019 (7)
  • 2018 (34)
  • 2017 (15)
  • 2016 (7)
  • 2015 (18)
  • 2014 (31)
  • 2013 (55)
  • 2012 (100)

Search

Subscribe




Copyright © 2025 Prabhu Sunderaraman.

Powered by WordPress and News.