当前位置: 首页 > 知识库问答 >
问题:

Spring MVC-不支持内容类型“应用程序/json”

邢飞昂
2023-03-14

我正在Eclipse Enterprise中通过Spring框架而不是Spring Boot编写一个MVC项目。使用Postman,我将向我的方法发送一个json对象:

@PutMapping(value = "/put_in_mail", 
        produces = MediaType.APPLICATION_JSON_VALUE,
        consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Mailbox> putInMailBox(@RequestBody Mail mail) {
    return service.putMailInInbox(mail);
}

但是在Eclipse中,我收到了这个错误:

Jul 11, 2022 5:08:10 PM org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver logException
WARNING: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported]

我认为这与我的pom.xml和依赖有关:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>laustrup</groupId>
  <artifactId>Mailbox</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Mailbox Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
         <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.13.3</version>
    </dependency>
    
  </dependencies>
  
  <build>
    <finalName>Mailbox</finalName>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.3.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

pom中是否有任何内容。xml我应该更改吗?

其他细节如网络。xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>frontcontroller</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>frontcontroller</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ctx="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/context/spring-mvc.xsd"
    >

    <ctx:annotation-config></ctx:annotation-config>
    
    <ctx:component-scan base-package="laustrup.controllers"></ctx:component-scan>
    <ctx:component-scan base-package="laustrup.models"></ctx:component-scan>
    <ctx:component-scan base-package="laustrup.services"></ctx:component-scan>
</beans>

共有1个答案

宗政财
2023-03-14

确保模型文件包含您试图以JSON格式发送的数据所需的所有getter和setter。如果模型文件中的所有内容都很好,请查看详细解决方案内容类型“应用程序/JSON”的链接,该链接在Spring MVC和jackson中不受支持

 类似资料: