2017-12-27 62 views
2

我想插入一個簡單的對象到數據庫,但在我的控制器上,我無法發送LocalDate參數。如何接收LocalDate作爲參數

錯誤說malformed request syntax, invalid request message framing, or deceptive request routing我很確定這個參數是LocalDate造成的。

我控制器

@RequestMapping(value = "/finance",method = RequestMethod.POST) 
    public String postFinance(@ModelAttribute("finance") 
     Finance finance, Model model){ 
     financeServices.targetActivate(finance); 
     model.addAttribute("targetSetStatus",true); 
     return "finance/finance"; 
} 

金融實體

@Entity 
public class Finance { 


    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 
    private LocalDate targetDate; 
    private Double targetAmount; 
    private LocalDate createdDate; 

     //getter setters // 

} 

編輯: - 我試圖從形式插入,所以我的形式看起來像: -

<form class="form-horizontal" id="productForm" 
th:action="@{/finance}" method="post"> 

     <div class="form-group"> 
      <div th:if="${targetSetStatus}"> 
       <div class="alert alert-success"> 
          <p>New Target Has Been Set</p> 
       </div> 
      </div> 
     </div> 

     <div class="form-group"> 
       <label class="control-label col-sm-3" 
       for="targetAmount">Target Amount:</label> 
      <div class="col-sm-9"> 
       <input type="number" class="form-control" 
           placeholder="Targeted Amount" 
           name="targetAmount" id="targetAmount"> 
     </div> 
     </div> 
     <div class="form-group"> 
      <label class="control-label col-sm-3" 
      for="targetDate">Target Date:</label> 
      <div class="col-sm-9"> 
      <input type="date" class="form-control" 
           placeholder="Targeted Date" 
           name="targetDate" id="targetDate"> 
     </div> 
     </div> 
     <div class="form-group"> 
      <div class="col-sm-offset-3 col-sm-9"> 
       <button type="submit" class="btn btn-default">Set Target</button> 
     </div> 
    </div> 
</form> 

如果我用java.util.Date代替LocalDate它可以工作,但我想使用LocalDate它提供的java 8 API。

+1

基地支持在未來的未來,請參閱[拉入請求和分辨率(https://github.com/spring-projects/spring-boot/pull/9930)。與此同時,[看到這個問題](https://stackoverflow.com/questions/44924656/how-to-register-global-databinding-for-localdate-in-spring-mvc)。 –

回答

0

您必須註冊一個自定義的JsonDeserializer。使用此代碼:

import com.fasterxml.jackson.core.JsonParser; 
import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.DeserializationContext; 
import com.fasterxml.jackson.databind.JsonDeserializer; 
import org.springframework.boot.jackson.JsonComponent; 

import java.io.IOException; 
import java.time.LocalDate; 
import java.time.format.DateTimeFormatter; 

@JsonComponent 
public class CustomJsonDeserializer { 

    /*public static class Serializer extends JsonSerializer<LocalDate> { 

     @Override 
     public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { 

     } 
    }*/ 

    public static class LocalDateDeserializer extends JsonDeserializer<LocalDate> { 

     @Override 
     public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { 

      String strDate = jsonParser.getValueAsString(); 

      if (strDate == null || strDate.isEmpty()) { 
       return null; 
      } 

      return LocalDate.parse(strDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")); 
     } 
    } 

} 

看看這個鏈接Custom JSON Serializers and Deserializers

+0

我正在使用彈簧MVC不彈簧引導。這仍然有效嗎?我寫了上面的類導入spring引導依賴關係,但它沒有工作。 –

+0

此鏈接可以幫助你[鏈接](http://magicmonster.com/kb/prg/java/spring/webmvc/jackson_custom.html) – Jialzate