@RequestBody注释在Spring中不起作用并给出NoSuchMethodError错误

时间:2017-10-06 08:38:11

标签: java angularjs spring spring-mvc spring-boot

通过角度我发送更新请求到控制器中的SPRING我有方法来映射这个请求但是在这个@RequestBody中无法映射到我的类并给出

错误:

**2017-10-06 12:42:43.496 ERROR 5856 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeFactory.constructType(Ljava/lang/reflect/Type;Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JavaType;] with root cause**

**java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeFactory.constructType(Ljava/lang/reflect/Type;Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JavaType;**

**at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:319) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]**

**at org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter.canRead(TypeConstrainedMappingJackson2HttpMessageConverter.java:63) ~[spring-hateoas-0.19.0.RELEASE.jar:na]**

但是我已经检查了 com.fasterxml.jackson.databind.type.TypeFactory 这是他们的方法

@Deprecated
public JavaType constructType(Type type, Class<?> contextClass) {
    TypeBindings bindings = (contextClass == null)
            ? TypeBindings.emptyBindings() : constructType(contextClass).getBindings();
    return _fromAny(null, type, bindings);
}

角度请求代码

function updateActivityByActivityID(ENDPOINTS, ActivityID, bodyJSON) {
        var url = ENDPOINTS.SERVICE_ADDRESS
            + ENDPOINTS.UPDATE_ACTIVITY_BY_ACTIVITY_ID
            + ActivityID;
        return HttpHandler.PUT(url, bodyJSON);
    }

url: http://localhost:8080/Integration/Activity/updateCron/14

bodyJSON: cronExpression:“0 0/1 * 1/1 *?*”

控制器代码

@ApiOperation(value = "update Activity scheduled expression")
@RequestMapping(method = RequestMethod.PUT, path = "/Integration/Activity/updateCron/{activityID}", produces = "application/json")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Success", response = String.class),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 403, message = "Forbidden"),
        @ApiResponse(code = 404, message = "Not Found"),
        @ApiResponse(code = 500, message = "Failure") })
@ResponseBody
public IntegrationProcessResultVO updateActivityScheduledExpression(
        @PathVariable("activityID") Long activityID,
        @RequestBody ScheduledSetupVO scheduledSetupVO)
        throws ActivityNotFoundException, IOException,
        ActivitySchedulerException {

    System.out.println("Activity ID : "+activityID);
    System.out.println("Request Body : "+scheduledSetupVO);

    IntegrationProcessResultVO integrationProcessResultVO = activityService
            .updateActivityCronExpression(activityID, scheduledSetupVO);

    return integrationProcessResultVO;
}

ScheduledSetupVO类

package com.data.integration.service.vo;

public class ScheduledSetupVO {

private String cronExpression;

public ScheduledSetupVO(String cronExpression) {
    super();
    this.cronExpression = cronExpression;
}

public ScheduledSetupVO() {
    super();
    // TODO Auto-generated constructor stub
}

public String getCronExpression() {
    return cronExpression;
}

public void setCronExpression(String cronExpression) {
    this.cronExpression = cronExpression;
}

@Override
public String toString() {
    // TODO Auto-generated method stub
    return super.toString();
}

} 的的pom.xml

<?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>
<name>dataintegrationservice</name>
<description>Data Integration</description>
<url>http://iquantifi.com/</url>
<groupId>com.data.integration</groupId>
<artifactId>dataintegrationservice</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<!-- Configures repository location for pentaho libraries -->
<repositories>
    <repository>
        <id>pentaho-releases</id>
        <url>https://public.nexus.pentaho.org/content/groups/omni/</url> <!-- Location changed Previous - http://repository.pentaho.org/artifactory/repo/ -->
    </repository>
    <repository>

        <id>clojars-releases</id>
        <url>http://clojars.org/repo/</url>
    </repository>
</repositories>



<!-- lookup parent from repository -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <pentaho.kettle.version>6.1.0.0-192</pentaho.kettle.version>
    <vertx.version>3.3.0</vertx.version>
    <commons.lang.version>3.4</commons.lang.version>
    <jtds.version>1.3.1</jtds.version>
    <pentaho-reporting-version>6.1.0.1-196</pentaho-reporting-version>
    <quartz.version>2.2.3</quartz.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-dbcp</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.jtds</groupId>
        <artifactId>jtds</artifactId>
        <version>${jtds.version}</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>4.7</version>
    </dependency>
    <dependency>
        <groupId>com.hazelcast</groupId>
        <artifactId>hazelcast</artifactId>
        <version>3.7</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>
    <dependency>
        <groupId>net.lingala.zip4j</groupId>
        <artifactId>zip4j</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- pentaho libraries -->
    <dependency>
        <groupId>pentaho-kettle</groupId>
        <artifactId>kettle-core</artifactId>
        <version>${pentaho.kettle.version}</version>
    </dependency>

    <dependency>
        <groupId>pentaho-kettle</groupId>
        <artifactId>kettle-engine</artifactId>
        <version>${pentaho.kettle.version}</version>
    </dependency>

    <dependency>
        <groupId>pentaho-kettle</groupId>
        <artifactId>kettle-ui-swt</artifactId>
        <version>${pentaho.kettle.version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libformula</artifactId>
        <version>${pentaho.kettle.version}</version>
    </dependency>

    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libbase</artifactId>
        <version>${pentaho.kettle.version}</version>
    </dependency>

    <!-- Jar required for Pentaho Transformation/job -->
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.12</version>
    </dependency>
    <dependency>
        <groupId>jsonpath</groupId>
        <artifactId>jsonpath</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-apache-client</artifactId>
        <version>1.16</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>2.5.15</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>
    <dependency>
        <groupId>com.enterprisedt</groupId>
        <artifactId>edtFTPj</artifactId>
        <version>2.1.0</version>
    </dependency>

    <!-- pentaho reporting -->


    <!-- Start pentaho reporting jar dependencies -->
    <dependency>
        <groupId>pentaho-reporting-engine</groupId>
        <artifactId>pentaho-reporting-engine-classic-core</artifactId>
        <version>${pentaho-reporting-version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>pentaho-reporting-engine</groupId>
        <artifactId>pentaho-reporting-engine-classic-extensions</artifactId>
        <version>${pentaho-reporting-version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libdocbundle</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libfonts</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libformat</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libloader</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>librepository</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libserializer</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libxml</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libswing</artifactId>
        <version>${pentaho-reporting-version}</version>
    </dependency>
    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.7</version>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.stripes</groupId>
        <artifactId>stripes</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>

    <dependency>
        <groupId>rhino</groupId>
        <artifactId>js</artifactId>
        <version>1.7R3</version>
    </dependency>
    <dependency>
        <groupId>org.jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.0.15</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>bsh</groupId>
        <artifactId>bsh</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.barbecue</groupId>
        <artifactId>barbecue</artifactId>
        <version>1.0.6d</version>
    </dependency>
    <dependency>
        <groupId>bsf</groupId>
        <artifactId>bsf</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>pentaho-library</groupId>
        <artifactId>libsparkline</artifactId>
        <version>5.0.3</version>
    </dependency>

    <!-- end of Jar required for Pentaho Transformation/job -->

    <!-- Apache commons util libraries -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons.lang.version}</version>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
    </dependency>
    <!-- Apache commons util libraries end -->
    <!-- Swagger UI -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.3.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.3.0</version>
        <scope>compile</scope>
    </dependency>
    <!-- end Swagger UI -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.7</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>
    <!-- Quartz Scheduler -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>

    <!-- Neo4j JDBC Driver -->
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-jdbc-driver</artifactId>
        <version>3.0</version>
    </dependency>

    <!-- MySQL connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- for NoSuchMethod Error while executing jar -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.3</version><!--$NO-MVN-MAN-VER$-->
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.3</version>
        </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.3</version>
        </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>true</excludeDevtools>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>
<scm>
    <url>https://bitbucket.org/kscloud/dataintegrationservice.git</url>
    <connection>scm:git:https://kaniket_kanaka@bitbucket.org/kscloud/dataintegrationservice.git</connection>
    <developerConnection>scm:git:https://kaniket_kanaka@bitbucket.org/kscloud/dataintegrationservice.git</developerConnection>
</scm>

如果我从控制器方法中删除了@RequestBody,那么它可以正常工作

1 个答案:

答案 0 :(得分:1)

您正在使用不兼容的Jackson版本。您自己管理版本不要那样做。

pom.xml移除spring-boot-starter-web之后已删除的以下依赖关系。 :

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.3</version><!--$NO-MVN-MAN-VER$-->
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </exclusion>
    </exclusions>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.3</version>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.3</version>
 </dependency>

或者至少从依赖项中删除<version>标记,以便Spring Boot可以管理版本。