@JsonCreator
public Foo( @JsonProperty("title") String title,
            @JsonProperty("strTags") Collection<String> strTags) {
        this.title = title;
        this.strTags = strTags;
}


和sig方法看起来像这样:

@RequestMapping(value = "/Preview", method = RequestMethod.POST)
public ModelAndView preview(@RequestBody final Foo foo) {..}


测试是:

String json = "\"foo\":{\"title\":\"test\",\"strTags\":[\"Tag1\",\"tag2\"]}";
MvcResult mvcResult =  this.mockMvc.perform(
  post("/Preview/").contentType(MediaType.APPLICATION_JSON).content(json))
    .andExpect(status().isOk())
    .andExpect(model().attributeExists("foo"))
    .andExpect(view().name("foo/preview"))
    .andDo(print())
    .andReturn();
}


但是,我得到了错误:

no suitable creator method found to deserialize from JSON String

最佳答案

属性titlestrTags应该是顶级的,如下所示:

String json = "{\"title\":\"test\",\"strTags\":[\"Tag1\",\"tag2\"]}";

10-06 05:56