2017-06-14 49 views
1

我操縱以下JSON對象在我的網站前端JSON對象和Spring @RequestParam

<script> 
window.campaign = { 
    name: "test", 
    budget: 20.00, 
    type: 0, 
    dynamic: false, 
    geo_target: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], 
    time_target: { 
     monday: ["00","00","00","00",1], 
     tuesday: ["00","00","00","00",1], 
     wednesday: ["00","00","00","00",1], 
     thursday: ["00","00","00","00",1], 
     friday: ["00","00","00","00",1], 
     saturday: ["00","00","00","00",1], 
     sunday: ["00","00","00","00",1] 
    }, 

    setName: function(val) { 
     this.name = val; 
    }, 

    //some more setters go here 
} 
</script> 

然後我把它發送到使用AJAX

$.ajax({ type: "POST", url: submit_url, data: JSON.stringify(window.campaign) }) 
.done(function() { alert("success"); }) 
.fail(function() { alert("error"); }); 

我的Spring應用程序現在的問題是映射所有變量都相應地在我的@Controller

.... 
@RequestParam(value = "name", required = false, defaultValue = "") String name, 
@RequestParam(value = "budget", required = false, defaultValue = "0") BigDecimal budget, 
@RequestParam(value = "type", required = false, defaultValue = "0") int type, 
@RequestParam(value = "dynamic", required = false, defaultValue = "false") boolean dynamic, 
@RequestParam(value = "geo_target", required = false, defaultValue = "") int[] geoTargeting, 
.... 

這一切工作正常,b UT我不知道如何映射time_target

我試圖創建一個新的Model和使用@RequestBody

.... 
@RequestParam(value = "name", required = false, defaultValue = "") String name, 
.... 
@RequestParam(value = "geo_target", required = false, defaultValue = "") int[] geoTargeting,  
@RequestBody TimeTarget timeTargeting 
.... 

沒有成功映射它。我用http://www.jsonschema2pojo.org爲我發送的整個對象創建了一個Class,但沒有任何成功(僅供參考,它創建了兩個類,一個用於timeTargeting,另一個用於其他所有內容)。

我真的很絕望。幫助,請:)如果提供的代碼不夠,我可以更新更多。

+0

1解決方案:'@RequestParam(value =「time_target」)String [] timeTargeting'這個工作給你? –

+1

這個控制器會變得非常混亂。你可以在一個POST參數中發送整個JSON字符串。然後在你的後端,你使用一個簡單的JSON庫來解析字符串並從中獲取數據。 – mumpitz

+1

我甚至懷疑它的作品。您正在發佈JSON而不是請求參數(afaik)。只需將JSON作爲一個主體發送,創建一個對象並將所有參數映射到該對象(傑克遜將爲您執行此操作)。 –

回答

1

您應該在Java中爲您的JSON結構創建一個包裝並將其作爲@RequestBody傳遞給控制器​​。這裏是我工作:

public class CampaignDTO { 
    private String name; 
    private BigDecimal budget; 
    private Integer type; 
    private Boolean dynamic; 

    @JsonProperty("geo_target") 
    private List<Integer> geoTarget; 

    @JsonProperty("time_target") 
    private TimeTarget timeTarget; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public BigDecimal getBudget() { 
     return budget; 
    } 

    public void setBudget(BigDecimal budget) { 
     this.budget = budget; 
    } 

    public Integer getType() { 
     return type; 
    } 

    public void setType(Integer type) { 
     this.type = type; 
    } 

    public Boolean getDynamic() { 
     return dynamic; 
    } 

    public void setDynamic(Boolean dynamic) { 
     this.dynamic = dynamic; 
    } 

    public List<Integer> getGeoTarget() { 
     return geoTarget; 
    } 

    public void setGeoTarget(List<Integer> geoTarget) { 
     this.geoTarget = geoTarget; 
    } 

    public TimeTarget getTimeTarget() { 
     return timeTarget; 
    } 

    public void setTimeTarget(TimeTarget timeTarget) { 
     this.timeTarget = timeTarget; 
    } 
} 

另一個DTO其中包括在CampainDTO:

public class TimeTarget { 
    private List<String> monday; 
    private List<String> tuesday; 
    private List<String> wednesday; 
    private List<String> thursday; 
    private List<String> friday; 
    private List<String> sunday; 

    public List<String> getMonday() { 
     return monday; 
    } 

    public void setMonday(List<String> monday) { 
     this.monday = monday; 
    } 

    public List<String> getTuesday() { 
     return tuesday; 
    } 

    public void setTuesday(List<String> tuesday) { 
     this.tuesday = tuesday; 
    } 

    public List<String> getWednesday() { 
     return wednesday; 
    } 

    public void setWednesday(List<String> wednesday) { 
     this.wednesday = wednesday; 
    } 

    public List<String> getThursday() { 
     return thursday; 
    } 

    public void setThursday(List<String> thursday) { 
     this.thursday = thursday; 
    } 

    public List<String> getFriday() { 
     return friday; 
    } 

    public void setFriday(List<String> friday) { 
     this.friday = friday; 
    } 

    public List<String> getSunday() { 
     return sunday; 
    } 

    public void setSunday(List<String> sunday) { 
     this.sunday = sunday; 
    } 
} 

而最後一部分是你的控制器。請注意,它在這裏作爲一個回聲 - 它會返回您發佈的內容。這就是爲什麼我在這裏添加了@ResponseBody。

@Controller 
public class CampainController { 

    @RequestMapping(name = "/test", method = RequestMethod.POST) 
    @ResponseBody 
    public CampaignDTO test(@RequestBody CampaignDTO campaignDTO) { 
     return campaignDTO; 
    } 
} 

將您的請求發送到http://localhost:8080/test後,它應該正常工作。

+0

將在一個小時左右嘗試,謝謝。 – mariobgr

+0

工作得很好。然而,我不得不改變'ajax'調用一點,以包括內容類型:「應用程序/ json」 – mariobgr

+0

我到達這裏,同時尋找不同的方式來解決[問題](https://stackoverflow.com/questions/ 49101657/spring-data-rest-call-to-save-receiving-object-with-null-variable)我剛在這裏發佈。這個解決方案看起來好像對我有用,但它沒有。 'save'方法接收的對象具有包含到數據庫中其他對象的URL的'String'變量。我實現的任何控制器方法都無法轉換這些URL。此轉換適用於擴展默認存儲庫方法,但前端無法訪問自定義存儲庫方法。 – GuiRitter