2017-06-14 46 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