JSON对象和Spring @RequestParam

时间:2017-06-14 16:22:49

标签: java json ajax spring spring-mvc

我正在我的网站前端操作以下JSON对象

<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将它发送到我的Spring应用程序

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

现在问题是在我的@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,
....

一切正常,但我不知道如何映射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,但没有任何成功(仅限FYI,它创建了两个类,一个用于timeTargeting,另一个用于其他所有类)。

我真的很绝望。请帮助:)如果提供的代码不够,我可以更新。

1 个答案:

答案 0 :(得分: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;
    }
}

CampainDTO中包含的另一个DTO:

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后,它应该可以正常运行。