JSON object and Spring @RequestParam



我正在我的网站前端操作以下 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,但没有成功(仅供参考,它创建了两个类,一个用于 timeTargeting,一个用于其他所有内容(。

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

你应该在 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 后,它应该可以正常工作。

最新更新