如何使用MappingJackson2HttpMessageConverter返回序列化对象



我正在学习Spring Framework,我的第一个目标是使用内置的序列化程序返回Version对象。

public class Version {
private int build;
private String releaseType;
public Version(int build, String releaseType) {
this.build          = build;
this.releaseType    = releaseType;
}
public int getBuild() {
return build;
}
public String getReleaseType() {
return releaseType;
}
public void setBuild(int build) {
this.build = build;
}
public void setReleaseType(String releaseType) {
this.releaseType = releaseType;
}
}

我的根类(我称之为Kernel)希望使用一个类进行应用程序配置

@EnableWebMvc
@Configuration
public class Kernel extends AbstractDispatcherServletInitializer implements WebMvcConfigurer {
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
annotationConfigWebApplicationContext.register(VersionController.class);
return annotationConfigWebApplicationContext;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/*" };
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
}
}

控制器

@RestController
public class VersionController {
@RequestMapping("/version")
@ResponseBody
public Version getVersion() {
return new Version(192837, "DEV");
}
}

我正在努力做到尽可能简单,我的项目中没有任何XML文件,因为我想完全使用Annotation&Java模式。

我从来没有真正喜欢过Spring Framework中的XML驱动概念,因为大多数时候,这些XML文件的内容看起来像是暴露的程序员垃圾,只有所有者才能理解如何设置。作为部署工作者公开响应序列化程序的配置有什么意义?我们不知道这是什么。

我得到的错误是:

HTTP Status 500 – Internal Server Error, No converter found for return value of type

我怀疑Jackson没有被调用是因为Spring不知道他应该在Version对象上使用它,但我不知道如何强迫Spring这样做。

我的pom.xml(使用Tomcat9作为web服务器)

<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.protean.league-craft</groupId>
<artifactId>league-craft</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<warName>lc</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Kernel</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

多亏了这个答案,我解决了问题https://stackoverflow.com/a/10650452/2010246

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
}

即使我在AbstractDispatcherServletInitializer中重写此方法,也不会像那样工作

相反,我不得不为MVC配置创建单独的类

package configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
@Configuration
public class Mvc extends WebMvcConfigurationSupport {
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
addDefaultHttpMessageConverters(converters);
}
@Bean
MappingJackson2HttpMessageConverter converter() {
return new MappingJackson2HttpMessageConverter();
}
}

现在它加载正确,我可以简单地返回类,就像我想一样

我想核心问题是没有注释为@Configuration并扩展WebMvcConfigurationSupport父的类

最新更新