代码之家  ›  专栏  ›  技术社区  ›  Bartłomiej Sobieszek

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

  •  0
  • Bartłomiej Sobieszek  · 技术社区  · 6 年前

    我正在学习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>
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Bartłomiej Sobieszek    6 年前

    由于这个答案,我解决了我的问题 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 起源