gpt4 book ai didi

java - Jackson2 objectmapper 未将请求映射到对象

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:51 25 4
gpt4 key购买 nike

调度程序-servlet.xml

        <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<context:component-scan base-package="com.demo" />
<context:annotation-config />

<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926" />

<mvc:resources mapping="/images/**" location="/resources/images/" cache-period="31556926" />

<mvc:resources mapping="/templates/**" location="/resources/templates/" cache-period="31556926" />

<mvc:annotation-driven/>

<tx:annotation-driven transaction-manager="txManager" />
<aop:aspectj-autoproxy />
<context:property-placeholder location="classpath:database.properties" />


<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.demo.model.CompetencyMapping</value>

</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</beans>

我在 AnnotationMethodHandlerAdapter 中使用了 MappingJackson2HttpMessageConverter。

pom.xml:刚刚添加了 com.fasterxml.jackson.core“databind”和“core”引用

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>pcompui</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>pcompui Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<spring.version>4.0.6.RELEASE</spring.version>
<hibernate.version>4.1.9.Final</hibernate.version>
<spring.security.core>3.0.5.RELEASE</spring.security.core>
<jackson.version>2.8.5</jackson.version>
</properties>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- Java Mail API -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>


<!-- joda time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.4</version>
</dependency>

<!-- java servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>

<!-- jstl jar -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>



<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>


<!-- AOP dependencies -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>

<!-- hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>





<!-- EHCache Core APIs -->
<!-- <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId>
<version>2.6.9</version> </dependency> -->

<!-- EHCache uses slf4j for logging -->
<!-- <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId>
<version>1.7.5</version> </dependency> -->

<!--connector jar for db -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>

<!-- JSON jar -->
<dependency>
<groupId>com.metaparadigm</groupId>
<artifactId>json-rpc</artifactId>
<version>1.0</version>
</dependency>

<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.core}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.core}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.core}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.core}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>



<!-- for logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>


<!-- mysql dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>

<!-- JACKSON Dependency -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>


</dependencies>
<build>
<finalName>pcompui</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

现在,当使用 json 发送请求时, Controller 方法不会被调用。如果我删除方法参数“@RequestBody DateRequest systemDate”,该方法将被调用,控制台显示语句。

使用的 Controller 是@RestController

//headers={"Accept=application/json", "Content-Type=application/json"}
@RequestMapping(value="/retrieveStartandEndDates",method=RequestMethod.POST,consumes={"application/json"} , headers={"Accept=application/json", "Content-Type=application/json"})
public RetrieveStartEndDates retrieveStartandEndDates(@RequestBody DateRequest systemDate){
//RetrieveStartEndDates systemDate = new RetrieveStartEndDates();
System.out.println("inside controller:::"+systemDate.getSystemDate());
RetrieveStartEndDates obj = new RetrieveStartEndDates();
obj = schemeService.getStartAndEndDates(systemDate.getSystemDate());
System.out.println(obj.toString());
return obj;
}

DateRequest 是一个简单的 DTO 对象

package com.demo.vo;

public class DateRequest {

private String SystemDate;
private String dest;
public String getSystemDate() {
return SystemDate;
}
public void setSystemDate(String systemDate) {
this.SystemDate = systemDate;
}
public String getDest() {
return dest;
}
public void setDest(String dest) {
this.dest = dest;
}

}

JSON 请求

{系统日期:“12/23/2016”,目标:“retrieveStartandEndDates”}

不确定我在这里错过了什么。如有任何建议/帮助,我们将不胜感激。

最佳答案

尝试在dispatcher-servlet.xml中使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter而不是org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

关于java - Jackson2 objectmapper 未将请求映射到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41303629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com