简体   繁体   中英

The request sent by the client was syntactically incorrect

i am calling a stored procedure using spring hibernate,here i have two tables called DataValueTable and employee.In post method when i post data from postclient i am getting

The request sent by the client was syntactically incorrect

Only for DataValueTable, post methods are not working.For employee all operations are performing well.Can anyone guide me why its showing error.

Controller method

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

Request format

{
"ID":1,
"Datatype": "asd",
"Datacategory": "assaa",
"DataValue": "asassasa"

}

如果要屏蔽控制器不必要的元素/属性,请尝试在DataValueTable类上使用以下annotation

  @JsonIgnoreProperties(ignoreUnknown = true) 

Try changing your properties in DataValueTable to below (using camel case) and then generate the getter and setter methods.

{
"id":1,
"dataType": "asd",
"dataCategory": "assaa",
"dataValue": "asassasa"
}

Update

Jackson is very powerful and restrictive as well. It enforces naming convention very well, and one way to use your own naming strategy is to extend

com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy

If that is not possible, annotate your property with @JsonProperty("MyJsonProperty") but in the first place you needed to follow the naming convention, so the original answer is more appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM