为什么这段代码无法使用Rest API进行任何POST请求?

huangapple go评论80阅读模式
英文:

Why this code can't POST anything using Rest API?

问题

这段代码为什么无法工作你看得明白吗?我无法通过addTopic函数将任何主题添加到我的列表中(我在末尾附上了错误消息)。

private List<Topic> topics = new ArrayList<>(Arrays.asList(
    new Topic("java", "back", "java description"),
    new Topic("html", "front", "html description")
));

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

@PostMapping("/topics")
public void addTopic(@RequestBody Topic topic) {
    topicService.addTopic(topic);
}

public class Topic {
    
    private String id;
    private String name;
    private String description;
    
    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;
    }
}

"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",

英文:

Do you see why this code doesn't work? I can't add any topic to my list through function addTopic (I get a error message attached at the end).

private List&lt;Topic&gt; topics = new ArrayList&lt;&gt;(Arrays.asList(
new Topic(&quot;java&quot;, &quot;back&quot;, &quot;java description&quot;),
new Topic(&quot;html&quot;, &quot;front&quot;, &quot;html description&quot;),
));
public void addTopic(Topic topic) {
topics.add(topic);
}	
@PostMapping(&quot;/topics&quot;)
public void addTopic(@RequestBody Topic topic) {
topicService.addTopic(topic);
}
public class Topic {
private String id;
private String name;
private String description;
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;
}
}

"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",

答案1

得分: 0

@PostMapping(path = "/**", consumes = "application/json", produces = "application/json")
public void addTopic(@RequestBody Topic topic) {
//code
}

英文:
@PostMapping(path = &quot;/***&quot;, consumes = &quot;application/json&quot;, produces = &quot;application/json&quot;)
public void addTopic(@RequestBody Topic topic) {
//code
}

huangapple
  • 本文由 发表于 2020年4月9日 17:46:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定