我正在使用spring hibernate调用存储过程,这里我有两个名为DataValueTable和employee的表。在post方法中,当我从postclient发布数据时我得到了
客户端发送的请求在语法上不正确
仅对于DataValueTable,post方法不起作用。对于员工而言,所有操作都表现良好。任何人都可以指导我显示错误的原因。
控制器方法
@Controller
@RequestMapping("/DataValueTable")
public class DataController {
@Autowired
DataServices dataServices;
static final Logger logger = Logger.getLogger(DataController.class);
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
Status addData(@RequestBody DataValueTable dataObject) {
try {
dataServices.addDataEntity(dataObject);
return new Status(1, "data added Successfully !");
} catch (Exception e) {
// e.printStackTrace();
return new Status(0, e.toString());
} }
DataValueTable.java
@Entity
@Table(name = "DataValueTable")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class DataValueTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "ID")
private long ID;
@Column(name = "Datatype")
private String Datatype;
@Column(name = "Datacategory")
private String Datacategory;
@Column(name = "DataValue")
private String DataValue;
public long getID() {
return ID;
}
public void setID(long ID) {
this.ID = ID;
}
public String getDatatype() {
return Datatype;
}
public void setDatatype(String Datatype) {
this.Datatype = Datatype;
}
public String getDatacategory() {
return Datacategory;
}
public void setDatacategory(String Datacategory) {
this.Datacategory = Datacategory;
}
public String getDataValue() {
return DataValue;
}
public void setDataValue(String DataValue) {
this.DataValue = DataValue;
}}
spring.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.beingjavaguys.controller" />
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost;database=Sample" />
<property name="username" value="scgg" />
<property name="password" value="rtyt" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.beingjavaguys.model.Employee</value>
<value>com.beingjavaguys.model.DataValueTable</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataDao" class="com.beingjavaguys.dao.DataDaoImpl"></bean>
<bean id="dataServices" class="com.beingjavaguys.services.DataServicesImpl"></bean>
</beans>
DataDoaImpl.java
@Override
public boolean addDataEntity(DataValueTable dataObject) throws Exception {
session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
int ival = session.createSQLQuery("EXEC createData @datatype=N'"+dataObject.getDatatype() +"',@datacategory=N'" + dataObject.getDatacategory() +"',@datavalue=N'" + dataObject.getDataValue() +"'").executeUpdate();
t.commit();
session.close();
return false;
}
请求格式
{
"ID":1,
"Datatype": "asd",
"Datacategory": "assaa",
"DataValue": "asassasa"
}
答案 0 :(得分:0)
如果要保护控制器免受不必要的元素/属性的影响,请尝试在DataValueTable类的annotation
下面使用。
@JsonIgnoreProperties(ignoreUnknown = true)
答案 1 :(得分:0)
尝试将DataValueTable
中的属性更改为以下(使用驼峰大小写),然后生成getter和setter方法。
{
"id":1,
"dataType": "asd",
"dataCategory": "assaa",
"dataValue": "asassasa"
}
<强>更新强>
杰克逊也非常强大和限制。它很好地强制执行命名约定,并且使用您自己的命名策略的一种方法是扩展
com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy
如果无法做到这一点,请使用@JsonProperty("MyJsonProperty")
注释您的媒体资源,但首先您需要遵循命名惯例,因此原始答案更合适。