如何使用MappingJackson2HttpMessageConverter

时间:2019-01-19 10:12:44

标签: spring spring-mvc serialization

我正在学习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;
    }
}

我的根类(我称为内核),很乐意使用一个类来保留应用程序配置

@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>

1 个答案:

答案 0 :(得分:0)

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

A

ALTER Procedure [dbo].[PTA_Processing_D365]
as

BEGIN TRY
BEGIN TRANSACTION test1

    update table A
    ....
    ......
    ......
    ;
    insert into table A
    (.....
    ....
    ....)
    Select .....
    ....
    ..... from table b
    where .....;

    select 1/0

COMMIT TRAN test1
END TRY

BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRAN test1
    INSERT INTO tableA_ErrorLog
               ([ExecutedDate]
               ,[ERROR_NUMBER]
               ,[ERROR_SEVERITY]
               ,[ERROR_STATE]
               ,[ERROR_PROCEDURE]
               ,[ERROR_LINE]
               ,[ERROR_MESSAGE])
    select GetDate (), ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_PROCEDURE(), ERROR_LINE(), ERROR_MESSAGE()
END CATCH

即使我在@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MappingJackson2HttpMessageConverter()); } 中重写此方法,也不会那样工作

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

AbstractDispatcherServletInitializer

现在它可以正确加载了,我可以像我想要的那样简单地返回类

我想核心问题是没有被注解为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父级的类