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 に答える