Produce.java(实体类)
@Column(name="productionStartFrom")
private DateTime productionStartFrom;
@Column(name="lastDateForBid")
private DateTime lastDateForBid;
@Column(name="produceDate")
private DateTime produceDate;
html代码
<div class="col-sm-4 form-group datepicker">
<input style="width: 200px;" type="text" class="form-control"
name="productionStartFrom" ng-model="produce.productionStartFrom" placeholder="Production Starts From"required />
</div>
这是我的控制器类,如果使用angular js type =“text”。我上面提到了错误。
@RequestMapping(value ={"/produce"}, method = RequestMethod.POST)
@ResponseBody
public Produce saveProduce(@RequestBody Produce produce,
Model model,HttpSession session,BindingResult bindingResult, HttpServletResponse
response,HttpServletRequest request) throws IOException, SQLException
{
System.out.println("Control to spring" + produce);
System.out.println(produce.getProductionStartFrom());
}
堆栈跟踪
2017-03-02 11:19:09 DEBUG DefaultHandlerExceptionResolver:134 - 解析处理程序中的异常[public com.tta.abcd.model.Produce com.tta.abcd.controller.ProduceController.saveProduce(com.tta.abcd) .model.Produce,org.springframework.ui.Model,javax.servlet.http.HttpSession,org.springframework.validation.BindingResult,javax.servlet.http.HttpServletResponse,javax.servlet.http.HttpServletRequest)抛出java.io. IOException,java.sql.SQLException]:org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法从String值('Thu Mar)实例化类型[simple type,class org.joda.time.DateTime]的值09 2017 00:00:00 GMT + 0530(IST)');没有单字符串构造函数/工厂方法 在[来源:org.apache.catalina.connector.CoyoteInputStream@13d0b05; line:1,column:19](通过引用链:com.tta.abcd.model.Produce [“productionStartFrom”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从String值实例化类型[simple type,class org.joda.time.DateTime]的值('Thu Mar 09 2017 00:00:00 GMT + 0530( IST)');没有单字符串构造函数/工厂方法 在[来源:org.apache.catalina.connector.CoyoteInputStream@13d0b05; line:1,column:19](通过引用链:com.tta.abcd.model.Produce [“productionStartFrom”]) 2017-03-02 11:19:09 DEBUG DispatcherServlet:999 - Null ModelAndView返回DispatcherServlet,名称为'dispatcher':假设HandlerAdapter已完成请求处理 2017-03-02 11:19:09 DEBUG DispatcherServlet:976 - 成功完成请求
答案 0 :(得分:2)
错误说:
无法实例化类型[simple type,class]的值 org.joda.time.DateTime]来自String值('Thu Mar 09 2017 00:00:00 GMT + 0530(IST)');没有单字符串构造函数/工厂方法
要解决此问题,您可以为您的实体设置setter方法,该方法将String作为输入并创建Joda DateTime对象。
public void setProductionStartFrom(String productionStartFrom) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
this.productionStartFrom = formatter.parseDateTime(productionStartFrom);
}