我正在学习Spring框架,我的第一个目标是使用内置序列化程序返回一个版本对象。
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;
}
}
我的根类(我称之为内核)希望使用一个类来进行应用程序配置
@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文件,因为我想完整地进行。
注解与Java
尽可能长时间使用。
我从来没有真正喜欢Spring框架中的XML驱动概念,因为大多数时候,这些XML文件的内容看起来像公开的程序员垃圾,除了所有者之外,没有人会理解如何设置。将响应序列化程序的配置公开为部署工作程序的意义是什么?将不知道这是什么。
我得到的错误是:
HTTP Status 500 â Internal Server Error, No converter found for return value of type
我怀疑杰克逊没有被调用,因为Spring不知道他应该在版本对象上使用它,但是我不知道如何强制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>