How to use fasterxml Jackson JSON serialization in Spring 4
Thanks to Sotirios, I've resolved my issue. I'm not sure why this resolved it, but it did.
I simply put the following in my servlet-context.xml:
<mvc:annotation-driven/>
That's it. That's all it took, and now Spring is returning the JSON representation of object that I expected.
John O'Conner
Updated on June 29, 2022Comments
-
John O'Conner almost 2 years
Why doesn't my Spring controller return media type
application/json
when I call it with the following curl:curl -v -i -H "Accept: application/json" localhost:8080/properties-ws/prop/2322
I've read all other questions and responses on this subject and none address my specific issue.
I'm using Spring 4, and have a simple Controller and pom.
Controller method
@RequestMapping(value="/prop/{character}", method=RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE}) public @ResponseBody CharData getData(@PathVariable(value="character") int codePoint) { CharData chData = null; chData = CharPropertiesService.getProperties(codePoint); return chData; }
POM file with the dependency on Jackson
I included the fasterxml jackson-databind artifact version 2.3.3
Here is the pom:
<?xml version="1.0" encoding="UTF-8"?> <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>com.joconner.unicode</groupId> <version>0.0.1-SNAPSHOT</version> <name>properties-ws</name> <artifactId>properties-ws</artifactId> <packaging>war</packaging> <properties> <java-version>1.7</java-version> <org.springframework-version>4.0.3.RELEASE</org.springframework-version> <org.aspectj-version>1.6.9</org.aspectj-version> <org.slf4j-version>1.6.1</org.slf4j-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.joconner.unicode</groupId> <artifactId>properties</artifactId> <version>1.0.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.3</version> </dependency> <!-- Testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> <scope>runtime</scope> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Spring Web MVC Features --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
The controller returns HTTP response 406 with a message:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
Any ideas about where I'm not configuring this properly?