1
@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
4

1 回答 1

3

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

String json = "{\"title\":\"test\",\"strTags\":[\"Tag1\",\"tag2\"]}";
于 2013-01-06T14:28:55.780 回答