Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用POST请求发送JSON_Java_Json_Spring_Post - Fatal编程技术网

Java 无法使用POST请求发送JSON

Java 无法使用POST请求发送JSON,java,json,spring,post,Java,Json,Spring,Post,这是我的控制器: @RequestMapping(value = "", method = RequestMethod.POST) public @ResponseBody ResponseEntity<String> create( @RequestBody Category category) { if (categoryService.create(category)) { return new ResponseEntity<St

这是我的控制器:

@RequestMapping(value = "", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<String> create(
        @RequestBody Category category) {

    if (categoryService.create(category)) {
        return new ResponseEntity<String>(HttpStatus.OK);
    } else {
        return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
    }
}
@RequestMapping(value=”“,method=RequestMethod.POST)
public@ResponseBody ResponseEntity创建(
@请求主体类别){
if(类别服务创建(类别)){
返回新的响应状态(HttpStatus.OK);
}否则{
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
}
这是我的配置:

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        </beans:list>
    </beans:property>
</beans:bean>

<context:component-scan base-package="ru.tenet.cafe" />

这是一个类别:

private Integer id;

private String title;

private String engTitle;

private String description; 

private List<MenuItem> menuItems;   

public Category()
{

}
public Category(Integer id, String title, String engTitle,
        String description, List<MenuItem> menuItems) {
    super();
    this.id = id;
    this.title = title;
    this.engTitle = engTitle;
    this.description = description;
    this.menuItems = menuItems;
}

// getters and setters
私有整数id;
私有字符串标题;
私人产权;
私有字符串描述;
私有列表菜单项;
公共类别()
{
}
公共类别(整数id、字符串标题、字符串标题、,
字符串说明,列表菜单项){
超级();
this.id=id;
this.title=标题;
this.engTitle=engTitle;
this.description=描述;
this.menuItems=menuItems;
}
//接球手和接球手
如果我尝试发送内容类型为application/json和以下正文的post请求:

{“id”:8,“title”:“Пцца”,“engTitle”:“Pizza”,“description”:null,“menuItems”:[{“id”:4,“title”:“Пццццццццц”,“engTitle”:“Pepperoni”,“price”:300.0,“ “一致性”:“E666, стальная стружка, вода (без “体积价值”:500.0,“体积价值”:“Γ”},{“id”:5,“标题”:“Маааааааааа”英语标题为“玛格丽塔”,“价格”:400.0,“说明”:“Саааа107 “一致性”:“一致性,一致性,一致性”, “volumeValue”:500.0,“VolumeTile”:“Γ”},{“id”:6,“标题”:“κааз”,“英语标题”:“Kavkaz” ji est,“价格”:300.0,“说明”:“БааПаа.СаМ” “一致性”:“一致性” “volumeValue”:500.0,“VolumeTile”:“Γ”}]}

我会得到:

HTTP状态415。服务器拒绝了此请求,因为请求失败 实体的格式不受请求的资源的支持 请求的方法

怎么了

UPD: 加上这个
@RequestMapping(value=”“,method=RequestMethod.POST,consumes=MediaType.APPLICATION\u JSON\u value,products=“APPLICATION/JSON”)
给了我同样的结果

我也遇到了同样的问题

我删除了@RequestBody,而使用了字符串。在您的情况下,它将是@Requestparam字符串类别

现在类别将是JSON格式的字符串。现在使用json去序列化器并反序列化json


注意:我承认这不是您当前问题的解决方案,但这是@RequestMapping中的另一种解决方案

Add
products=“application/json”
。当请求add
consumes=MediaType时,也在请求中添加头。在
@RequestMapping
注释中的应用程序\u JSON\u值
没有帮助。同样的事情。@ViswanathLekshmanan,什么样的头球?内容类型?是的。在中添加内容类型request@Tony你照我说的做了吗?默认情况下,如果内容类型是@SpringLearner,我会试试。我必须添加org.json jar并创建新的构造函数来使用json。