Home > Mobile >  Spring MVC - Content type 'application/json' not supported
Spring MVC - Content type 'application/json' not supported

Time:07-12

I'm writing an MVC project through Spring framework, not Spring Boot, in Eclipse Enterprise. With Postman I'm sending a json object to my method:

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

But in Eclipse I'm getting this error:

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]

I think it has something to do with my pom.xml and my dependencies:

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

It there anything in pom.xml I should change?

Other details are such - web.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>

CodePudding user response:

As Sotirios Delimanolis mentioned, I missed a mvc:annotation tag in my -servlet.xml file and wrote something wrong about the schemaLocation, now json is supported with mvc.

CodePudding user response:

Make sure the Model file contains all the required getters and setters for the data you are trying to send in JSON format. If everything inside the Model file is fine do take a look at the link for a detailed solution Content type 'application/JSON not supported in Spring MVC and jackson

  • Related