0

私はジャクソンで非常に簡単なテストを行っています。クラスがあり、そのオブジェクトをパラメータとして使用し、Jersey メソッドの値を返します。クラスは次のとおりです。

import java.util.List;

public class TestJsonArray {

    private List<String> testString;

    public List<String> getTestString() {
        return testString;
    }

    public void setTestString(List<String> testString) {
        this.testString = testString;
    }
}

リスト テスト文字列に 1 つの文字列を追加しようとするジャージー メソッドがあります。

@Path("/arrayObj")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object createObjectArray(@QueryParam("param") String object) throws JsonGenerationException, JsonMappingException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        TestJsonArray convertValue = objectMapper.convertValue(object, TestJsonArray.class);
        convertValue.getTestString().add("hello");
        return objectMapper.writeValueAsString(convertValue);
    }

パラメータを指定してこのメ​​ソッドを呼び出すと

{"testString":["こんにちは"]}

例外があります:

java.lang.IllegalArgumentException: Can not construct instance of test.rest.TestJsonArray, problem: no suitable creator method found to deserialize from JSON String
 at [Source: N/A; line: -1, column: -1]

逆シリアル化プロセスで例外がスローされます。

TestJsonArray convertValue = objectMapper.convertValue(オブジェクト, TestJsonArray.class);

なぜこの例外がスローされるのか疑問に思っています。私は何を間違っていますか?

4

1 に答える 1

5

の代わりに のreadValue方法を試すObjectMapperconvertValue

objectMapper.readValue(json, TestJsonArray.class);

これはうまくいくはずです。

于 2013-05-17T09:32:35.373 に答える