无法使用POST请求发送JSON

时间:2016-06-15 11:24:47

标签: java json spring post

这是我的控制者:

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

这是我的配置:

    <!-- 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

如果我尝试使用Content-type:application / json和以下正文发送帖子请求:

  

{ “ID”:8中, “标题”: “Пицца”, “engTitle”: “披萨”, “描述”:空 “的菜单项”:[{ “ID”:4 “标题”:“Пепперони “ ”engTitle“: ”辣“, ”价格“:300.0, ”说明“:” Сами   лючщипицаслющи。 Толькищтопривезлидарагой。“,”containsOf“:”E666,   стальнаястружка,вода(без   ГМО) “ ”volumeValue“:500.0, ”volumeTitle“: ”г“},{ ”ID“:5 ”标题“: ”Маргарита“, ”engTitle“: ”玛格丽塔“, ”价格“:400.0,” 说明“:” Сами   сочнипицаслющи。 Мамайклянус。“,”containsOf“:”Перец,сыр,колбаска,   ногти “ ”volumeValue“:500.0, ”volumeTitle“: ”г“},{ ”ID“:6中, ”标题“: ”Кавказ“, ”engTitle“:” 高加索   ji est“,”price“:300.0,”description“:”Вахпица。 Самем   дарагой。“,”“withinOf”:“Ароматизатор\”Гусь\“идентичный   натуральному”, “volumeValue”:500.0, “volumeTitle”: “г”}]}

我会得到:

  

HTTP状态415.服务器因请求而拒绝了此请求   实体的格式不受所请求资源的支持   要求的方法。

有什么问题?

UPD: 添加这个 @RequestMapping(value = "", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE,produces = "application/json")给了我相同的结果

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题

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

现在类别将是JSON格式的String。现在使用json desrializer并反序列化json

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

相关问题