如何在REST中创建POST请求以接受JSON输入? [英] How to create a POST request in REST to accept a JSON input?

查看:710
本文介绍了如何在REST中创建POST请求以接受JSON输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习RESTful Web服务。我正在创建一组简单的Web服务。我开始处理POST时卡住了。

I am trying to learn RESTful web services. And am creating a simple set of web services. Got stuck when I started working on POST.

我想将JSON输入传递给POST方法。这就是我在代码中所做的:

I want to pass JSON input to a POST method. This is what I did in the code:

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes="application/x-www-form-urlencoded", produces="text/plain")
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody String createChangeRequest(MyCls mycls) {
    return "YAHOOOO!!"; 
}

我在我的POM.xml中包含了Jackson。

I included Jackson in my POM.xml.

 <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-lgpl</artifactId>
    <version>1.9.13</version>
</dependency>   

MyCls是一个简单的类,有几个getter和setter。

MyCls is a simple class with a few getters and setters.

我从chrome的简单REST客户端调用上面的POST服务。

I am calling the above POST service from chrome's simple REST client.

URL: http://localhost:8080/MYWS/cls/create
Data: {<valid-json which corresponds to each variable in the MyCls pojo}

我看到以下回复:

415 Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

我尝试在REST客户端的POST请求中添加标题为application / json - 但是没有帮助。

I tried adding header as "application/json" in the POST request in the REST client - but that did not help.

有人能让我知道我在这里缺少什么吗?如何自动将输入JSON映射到MyCls pojo?我在这里错过了任何配置吗?

Can someone let me know what I am missing here? How can I automatically map my input JSON to the MyCls pojo? Am I missing any configuration here?

编辑:
MyCls.java

MyCls.java

public class MyCls{
   private String name;
   private String email;
   private String address;
       public String getName() {
    return name;
   }
   public void setName(String name) {
    name= name;
   }
       ---similar getter and setter for email, address--
}


$来自chrome简单REST客户端的b $ b

json:

json from chrome Simple REST Client:

{"name":"abc", "email":"de@test","address":"my address"}

编辑:
将我的控制器方法更改为以下内容,但仍然看到相同的错误:

Changed my controller method to the following, but still see the same error:

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes="application/json", produces="text/plain")
@ResponseStatus(HttpStatus.CREATED)
 public @ResponseBody String createChangeRequest(@RequestBody MyCls mycls) {
  return "YAHOOOO!!"; 
 }


推荐答案

假设您的客户正在发送 application / json 作为其内容类型,然后处理程序映射到

Assuming your client is sending application/json as its content type, then a handler mapped to

consumes="application/x-www-form-urlencoded"

将无法处理它。实际的内容类型与预期的不匹配。

will not be able to handle it. The actual Content-type doesn't match the expected.

如果你期望 application / json ,你应该

consumes="application/json"

另外,声明

public @ResponseBody String createChangeRequest(MyCls mycls) {

是(在默认环境中)相当于

is (in default environment) equivalent to

public @ResponseBody String createChangeRequest(@ModelAttribute MyCls mycls) {

这意味着 MyCls object是从请求参数创建的,而不是从JSON主体创建的。相反,你应该

This means the MyCls object is created from request parameters, not from JSON body. Instead, you should have

public @ResponseBody String createChangeRequest(@RequestBody MyCls mycls) {

以便Spring将您的JSON反序列化为 MyCls 类型的对象。

so that Spring deserializes your JSON to an object of type MyCls.

这篇关于如何在REST中创建POST请求以接受JSON输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆