Spring5 requestbody 415 ошибка с JSON

Я сделал простой пружинный контроллер. Когда я пробую тест с помощью керла или почтальона, он возвращает ошибку 415.

<!doctype html><html lang="en"><head><title>HTTP Status 415 – 
Unsupported Media Type</title><style type="text/css">h1 {font- 
family:Tahoma,Arial,sans-serif;color:white;background- 
color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans- 
serif;color:white;background-color:#525D76;font-size:16px;} h3 {font- 
family:Tahoma,Arial,sans-serif;color:white;background- 
color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans- 
serif;color:black;background-color:white;} b {font- 
family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p 
{font-family:Tahoma,Arial,sans-serif;background:white;color:black;font- 
size:12px;} a {color:black;} a.name {color:black;} .line 
{height:1px;background-color:#525D76;border:none;}</style></head><body> 
<h1>HTTP Status 415 – Unsupported Media Type</h1><hr class="line" /><p> 
<b>Type</b> Status Report</p><p><b>Description</b> The origin server is 
refusing to service the request because the payload is in a format not 
supported by this method on the target resource.</p><hr class="line" /> 
<h3>Apache Tomcat/9.0.6</h3></body></html>

Я ищу много сообщений в stackru; но это не сработало для меня.

  1. добавить библиотеку Джексона
  2. добавить Content-Type
  3. мвн чистый

.... как я могу это исправить?

мой curl линия

curl -i -X POST -H "Content-Type: application/json, charset=UTF-8" -d 
'{"user_key":"abcd", "type":"text", "content":"hello"}' 
http://localhost:8080/message

мой контроллер

@RestController
public class MessageController {
    @PostMapping(value = "message", consumes = 
MediaType.APPLICATION_JSON_VALUE)
    public String homeKeyBoardApi(@RequestBody RequestMessageVO vo) {
    System.out.println(vo);

    return "{\"text\": \"hello lolbot\"}";
    }
}

RequestMessageVO is

public class RequestMessageVO implements Serializable{

    private static final long serialVersionUID = -1L;

    private String user_key;
    private String type;
    private String content;

    public RequestMessageVO(){

    }

    public RequestMessageVO(String user_key, String type, String content) {
        this.user_key = user_key;
        this.type = type;
        this.content = content;
    }
// getter, setter...

мой web.xml является

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
version="3.0">
<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

Я уже включаю jackson.core в моем pom.xml

    //.....
    <!-- Jackson2 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.4</version>
    </dependency>

0 ответов

Другие вопросы по тегам