reactdontact
reactdontact

Reputation: 53

SpringBoot :Failed to read HTTP message

Service Class -

package org.sameer.learnSpringBoot.topic;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class TopicService {

    private List<Topic> topics = new ArrayList<>(Arrays.asList(new Topic("Spring", "Spring Framework", "Description for Spring"),
            new Topic("Hibernate", "Hibernate Framework", "Description For Hibernate"),
            new Topic("CoreJava", "Core Java Framework", "Description For CoreJava"),
            new Topic("Servlets", "Servlets Framework", "Description for Servlets")));

    public List<Topic> getAllTopic() {
        return topics;
    }

    public Topic getTopic(String id) {

        return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
    }

    public void addTopic(Topic topic) {
        topics.add(topic);

    }

}

Controller Class -

@RequestMapping(method = RequestMethod.POST ,value="/topics")
   public void addTopic(@RequestBody Topic topic) {
    topicService.addTopic(topic);

  }

Model Class Topic -

package org.sameer.learnSpringBoot.topic;

public class Topic {

    private String id;
    private String name;
    private String description;

    public Topic() {

    }

    public Topic(String id, String name, String description) {
        super();
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

I am not able to post using POSTMAN plugin from chrome. enter image description here

And Getting the same in Satacktrace -

2018-02-17 15:57:59.841  WARN 4328 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void org.sameer.learnSpringBoot.topic.TopicController.addTopic(org.sameer.learnSpringBoot.topic.Topic)
2018-02-17 15:58:08.501  WARN 4328 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void org.sameer.learnSpringBoot.topic.TopicController.addTopic(org.sameer.learnSpringBoot.topic.Topic)

I am learning Spring Boot. I am trying to HTML Put values to my topics page CAN ANYBODY PLEASE LET ME KNOW WHAT I AM MISSING HERE??? GET WORKS FINE STS -4.7.1 JRE - 1.8

Upvotes: 1

Views: 11647

Answers (1)

smruti ranjan
smruti ranjan

Reputation: 781

Your request body is missing so you are getting BAD REQUEST Error. in request header u have set "Content-type" as application/json. There is a tab named body next to it. click on that and provide the json input body for Topic.

 {
"id":"102",
"name":"math",
"description":"asdf"
}

enter image description here

Upvotes: 1

Related Questions