【发布时间】:2019-06-13 10:18:50
【问题描述】:
我正在学习 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>
【问题讨论】:
-
我认为你需要在你的版本 pojo 中有设置器
-
没有帮助,我听说我只需要吸气剂
-
你能像这样在请求映射中添加生产吗@RequestMapping(value = "/version",produces = "application/json") 并尝试
-
这些都不起作用,但添加生产参数会导致 406 http 错误
标签: spring spring-mvc serialization