2014-09-20 80 views
0

我在使用@OneToMany綁定和Java Play中的bindFromRequest方法時遇到問題。目前,我有,有許多FinancialAsset車型Java與JSON玩複雜數據綁定

@Entity 
public class Client extends Model { 

... 
@OneToMany(cascade = CascadeType.ALL, mappedBy = "client") 
public List<FinancialAsset> assetList; 
... 
} 

這裏是我的FinancialAsset模型的要點客戶端模型

@Entity 
public class FinancialAsset extends Model { 

@Id 
public long id; 

@ManyToOne(optional = false) 
@JoinColumn(name="client", referencedColumnName = "id") 
public Client client; 

public enum AssetType { 
    K401, ANNUITY, CASH, CD, GOLD, IRA, PARTNERSHIP, RENTAL_PROP, RETIREMENT_ACCT 
} 

public float totalValue; 
public AssetType realAssetType; 

public String financialInstitute; 

public String description; 

public void setRealAssetType (String assetTypeString) { 
    this.realAssetType = AssetType.valueOf(assetTypeString); 
} 

public static List<AssetType> getAllAssetTypes() { 
    List<AssetType> all = new ArrayList<AssetType>(Arrays.asList(AssetType.values())); 
    return all; 
} 

public static Finder<Long,FinancialAsset> find = new Finder(Long.class, FinancialAsset.class); 

public static List<FinancialAsset> allForClient(Client client) { 
    return find.where().eq("clientId", client.id).findList(); 
} 
} 

當我的前端客戶端(Angular.js)調用我的方法來更新客戶端具有下列數據:

{ 
    id: 1234, 
    assetList: Array[1] 
     assetType: "ANNUITY", 
     totalValue: 50000, 
     description: "blah", 
     financialInstitue: "blahstitute", 
    otherClientproperties: "..." 
} 

它試圖bindFromRequest但錯誤,當我做一個get()獲取客戶端:

@BodyParser.Of(play.mvc.BodyParser.Json.class) 
public static Result editClientJSON() { 
    Logger.debug("Reached editClientJSON"); 
    Form<Client> clientForm = Form.form(Client.class); 
    //Error is here on the get 
    Client client = clientForm.bindFromRequest().get(); 

    client.update(); 
    Logger.debug("Client updated: " + client.name); 
    response().setHeader(LOCATION, routes.ClientCtrl.getClientJSON(client.id).url()); 

    return ok(); 
} 

錯誤輸出:

play.api.Application$$anon$1: Execution exception[[IllegalStateException: No value]] 
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1] 
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.1] 
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:144) [play_2.10.jar:2.1.1] 
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:140) [play_2.10.jar:2.1.1] 
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1] 
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1] 
java.lang.IllegalStateException: No value 
at play.libs.F$None.get(F.java:540) ~[play_2.10.jar:2.1.1] 
at play.data.Form.get(Form.java:525) ~[play-java_2.10.jar:2.1.1] 
at controllers.ClientCtrl.editClientJSON(ClientCtrl.java:60) ~[na:na] 
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$14$$anonfun$apply$14.apply(routes_routing.scala:225) ~[na:na] 
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$14$$anonfun$apply$14.apply(routes_routing.scala:225) ~[na:na] 
at play.core.Router$HandlerInvoker$$anon$6$$anon$2.invocation(Router.scala:164) ~[play_2.10.jar:2.1.1] 

我的想法是,它有麻煩綁定資產列表,以FinancialAsset模型。我不知道如何調試或找出它的需求。

在此先感謝!

回答

0

這似乎是一個驗證問題。在從表單獲取底層對象之前,您應該檢查表單錯誤,因爲綁定可能失敗,並且可能沒有潛在對象要獲取。試試下面的代碼來檢測並顯示任何形式的錯誤:

@BodyParser.Of(play.mvc.BodyParser.Json.class) 
public static Result editClientJSON() { 
    Logger.debug("Reached editClientJSON"); 
    Form<Client> clientForm = Form.form(Client.class).bindFromRequest(); 

    if(clientForm.hasErrors()) {//check out for form errors 
     for (String errorKey : clientForm.errors().keySet()) { 
      for (ValidationError error : clientForm.errors().get(errorKey)) { 
       Logger.error(error.key() + " = " + error.message());     
      } 
     } 
     return badRequest(); 
    } 

    //There is no error on the form so it is now safe to get the Client 
    Client client = clientForm.get(); 
    client.update(); 
    Logger.debug("Client updated: " + client.name); 
    response().setHeader(LOCATION, routes.ClientCtrl.getClientJSON(client.id).url()); 

    return ok(); 
} 
+0

謝謝你的幫助。不幸的是,該表單似乎沒有任何錯誤。調試允許我一直到clientForm.get(),它繼續拋出相同的錯誤。在調試時我注意到它似乎認爲表單沒有數據。該請求似乎有一個json部分下的數據,但在urlFormEncoded或xml下它說「無」。 「無」也是表單在bindFromRequest之後認爲它具有的內容。我想知道如果這可能是一個註釋問題或JSON問題。 – gmills82 2014-09-23 00:30:50

+0

挖掘更深入的調試,它似乎既不綁定也不bindFromRequest被調用。如果我評估請求()。body()。asJson()我得到正確的數據在PUT中發送。但由於某些原因,它不會將這些數據放入clientForm中。 – gmills82 2014-09-23 01:14:52

0

這種行爲似乎是玩我的版本是用,玩2.1.1的錯誤。我升級到2.3,問題解決了。非常令人沮喪。