1

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

3
  • 1
    Well, you are not sending any body along the POST request... Commented Feb 17, 2018 at 10:45
  • @luk2302 Can you please elaborate ?? Commented Feb 17, 2018 at 10:50
  • Not really, look at your screenshot of postman, you are only sending one header, no body - what is your Spring application supposed to do other than reject that request? Commented Feb 17, 2018 at 10:51

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.