gpt4 book ai didi

java - 无法使用 POST 请求发送 JSON

转载 作者:行者123 更新时间:2023-11-30 10:43:15 25 4
gpt4 key购买 nike

这是我的 Controller :

@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 和以下正文发送 post 请求:

{"id":8,"title":"Пицца","engTitle":"Pizza","description":null,"menuItems":[{"id":4,"title":"Пепперони","engTitle":"Pepperoni","price":300.0,"description":"Сами лючщи пица слющи. Тольки щто привезли дарагой.","consistOf":"E666, стальная стружка, вода (без ГМО)","volumeValue":500.0,"volumeTitle":"г"},{"id":5,"title":"Маргарита","engTitle":"Margarita","price":400.0,"description":"Сами сочни пица слющи. Мамай клянус.","consistOf":"Перец, сыр, колбаска, ногти","volumeValue":500.0,"volumeTitle":"г"},{"id":6,"title":"Кавказ","engTitle":"Kavkaz ji est","price":300.0,"description":"Вах пица. Сам ем дарагой.","consistOf":"Ароматизатор \"Гусь\" идентичный натуральному","volumeValue":500.0,"volumeTitle":"г"}]}

我会得到:

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

怎么了?

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

最佳答案

我遇到了同样的问题

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

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

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

关于java - 无法使用 POST 请求发送 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com