SpringBoot:无法读取HTTP消息

时间:2018-02-17 10:43:13

标签: java spring-mvc spring-boot

服务类 -

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);

    }

}

控制器类 -

@RequestMapping(method = RequestMethod.POST ,value="/topics")
   public void addTopic(@RequestBody Topic topic) {
    topicService.addTopic(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;
    }

}

我无法使用chrome发布的POSTMAN插件。 enter image description here

在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)

我正在学习Spring Boot。我正在尝试HTML将值放到我的主题页面 任何人都可以让我知道我在这里失踪了什么???工作精细 STS -4.7.1 JRE - 1.8

1 个答案:

答案 0 :(得分:0)

您的请求正文已丢失,因此您收到了BAD REQUEST错误。在请求标题中你已设置&#34;内容类型&#34;作为application / json。旁边有一个名为body的选项卡。单击它并为Topic提供json输入体。

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

enter image description here