JsonParseException:MockMVC和Maven的UTF-8中间字节0x20无效

时间:2016-02-24 09:05:08

标签: java spring maven jackson spring-restdocs

编辑:如果我删除了spring-restdoc代码,那么测试将通过maven传递。

我试图在Spring Boot网络应用程序中使用MockMVC编写集成测试。

我现在只有一个测试(下面的步骤和代码),如果我在IDE中运行它,但是如果由Maven执行则会失败并返回以下堆栈跟踪:

org.springframework.restdocs.payload.PayloadHandlingException: com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0x20
 at [Source: [B@3793896e; line: 30, column: 23]
        at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidOther(UTF8StreamJsonParser.java:3223)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidOther(UTF8StreamJsonParser.java:3230)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._decodeUtf8_3fast(UTF8StreamJsonParser.java:3036)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._finishString2(UTF8StreamJsonParser.java:2216)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._finishString(UTF8StreamJsonParser.java:2165)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.getText(UTF8StreamJsonParser.java:279)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:441)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:574)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:423)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapArray(UntypedObjectDeserializer.java:510)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:437)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:552)
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:423)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2230)
        at org.springframework.restdocs.payload.JsonContentHandler.readContent(JsonContentHandler.java:81)
        at org.springframework.restdocs.payload.JsonContentHandler.findMissingFields(JsonContentHandler.java:49)
        at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:112)
        at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:73)
        at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
        at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:101)
        at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:158)

代码:

我添加了starter-test依赖项,并为将来的所有测试创建了一个超类:

@Ignore
@MappedSuperclass
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@ActiveProfiles(value = {"test"})
public class IntegrationTest
{
    @Autowired
    WebApplicationContext context;

    MockMvc mockMvc;

    @Rule
    public RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");

    @Before
    public void setUp()
    {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).build();
    }
}

然后我创建了这个测试:

public class PostsTest extends IntegrationTest
{
    @Test
    public void getAll() throws Exception
    {
        mockMvc.perform(get("/api/v1/posts").accept(MediaType.APPLICATION_JSON_VALUE)
            .andExpect(status().isOk())
            .andDo(document("posts-index", preprocessResponse(prettyPrint()), responseFields(
                fieldWithPath("data").description("The response payload"),
                fieldWithPath("data.[].id").description("ID"),
                fieldWithPath("data.[].title").description("Title"),
                fieldWithPath("data.[].body").description("Content"),
                fieldWithPath("data.[].type").description("Type"),
                fieldWithPath("data.[].createdAt").description("Creation date"),
                fieldWithPath("data.[].modifiedAt").description("Last edit date"),
                fieldWithPath("data.[].entityType").description("Entity type"),
                fieldWithPath("meta").description("The response metadata")
            )));
    }
}

2 个答案:

答案 0 :(得分:3)

我发现从restdocs代码中删除了preprocessResponse(prettyPrint()),测试通过了。

编辑:正如Andy Wilkinson在评论中所报道的那样,此错误已通过1.0.2.BUILD-SNAPSHOT的版本spring-restdocs修复。

答案 1 :(得分:-2)

你正在解析json,我怀疑它是从文件中读取的。

该文件似乎以错误的编码保存,例如ISO-8859-1。确保它以UTF-8,UTF-16或UTF-32保存。大多数JSON解析器都会猜测前四个字节的编码,如果找到不受支持的编码,则会失败。