14

Hi I am starting with Web Services in Spring, so I am trying to develop small application in Spring + JSON + Hibernate. I have some problem with HTTP-POST. I created a method:

@RequestMapping(value="/workers/addNewWorker", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public String addNewWorker(@RequestBody Test test) throws Exception {
    String name = test.name;
    return name;
}

And my model Test looks like:

public class Test implements Serializable {

private static final long serialVersionUID = -1764970284520387975L;
public String name;

public Test() {
}
}

By POSTMAN I am sending simply JSON {"name":"testName"} and I always get error;

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

I imported Jackson library. My GET methods works fine. I don't know what I'm doing wrong. I am grateful for any suggestions.

4

5 回答 5

28

使用将您的 JSON 对象转换为 JSON 字符串

JSON.stringify({"name":"testName"})

或手动。@RequestBody 期待 json 字符串而不是 json 对象。

注意:stringify 函数在某些 IE 版本中存在问题,firefox 可以使用

验证 POST 请求的 ajax 请求的语法。ajax 请求中需要processData:false属性

$.ajax({ 
    url:urlName,
    type:"POST", 
    contentType: "application/json; charset=utf-8",
    data: jsonString, //Stringified Json Object
    async: false,    //Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation
    cache: false,    //This will force requested pages not to be cached by the browser  
     processData:false, //To avoid making query String instead of JSON
     success: function(resposeJsonObject){
        // Success Action
    }
});

控制器

@RequestMapping(value = urlPattern , method = RequestMethod.POST)

public @ResponseBody Test addNewWorker(@RequestBody Test jsonString) {

    //do business logic
    return test;
}

@RequestBody- 将 Json 对象转换为 java

@ResponseBody- 将 Java 对象转换为 json

于 2013-09-01T03:40:14.950 回答
1

Test您需要为模型类中定义的所有字段包含 getter 和 setter——

public class Test implements Serializable {

    private static final long serialVersionUID = -1764970284520387975L;

    public String name;

    public Test() {

    }

    public String getName() {
        return name;
    }

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

}
于 2018-07-08T03:05:59.090 回答
0

尝试改用 application/* 。并使用 JSON.maybeJson() 检查控制器中的数据结构。

于 2013-08-31T17:01:38.363 回答
0

如果您想使用 json 作为 http 请求和响应,请执行以下操作。所以我们需要在 [context].xml 中进行更改

<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="jsonMessageConverter"/>
        </beans:list>
    </beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>   

MappingJackson2HttpMessageConverter 到 RequestMappingHandlerAdapter messageConverters 以便 Jackson API 启动并将 JSON 转换为 Java Bean,反之亦然。通过此配置,我们将在请求正文中使用 JSON,我们将在响应中接收 JSON 数据。

我还为控制器部分提供了小代码片段:

    @RequestMapping(value = EmpRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)

    public @ResponseBody Employee getDummyEmployee() {
    logger.info("Start getDummyEmployee");
    Employee emp = new Employee();
    emp.setId(9999);
    emp.setName("Dummy");
    emp.setCreatedDate(new Date());
    empData.put(9999, emp);
    return emp;
}

所以在上面的代码中 emp 对象将直接转换为 json 作为响应。帖子也会发生同样的情况。

于 2017-04-19T11:56:04.133 回答
0

这里

映射请求的可消耗媒体类型,缩小主映射。

生产者用于缩小主映射,您发送请求应指定确切的标头以匹配它。

于 2019-05-09T11:23:54.317 回答