How to resolve spinjar.com.jayway.jsonpath.PathNotFoundException: No results for path: xxx

I decided resolve that with a ‘Configuring Data Formats’ spi, like this:

public class DataFormatConfiguratorImpl implements DataFormatConfigurator {

    @Override
    public Class getDataFormatClass() {
        return JacksonJsonDataFormat.class;
    }

    @Override
    public void configure(DataFormat dataFormat) {
        if (dataFormat instanceof JacksonJsonDataFormat) {
            JacksonJsonDataFormat jsonDataFormat = (JacksonJsonDataFormat) dataFormat;
            jsonDataFormat.setJsonPathConfiguration(
                    Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS)
            );
        }
    }
}

After that i got expect result!

@Test
public void testWithSpinJsonPath() {
    JsonValue jsonValue = SpinValues.jsonValue(json).create();
    SpinJsonNode jsonNode = jsonValue.getValue();

    // cause exception: org.camunda.spin.json.SpinJsonDataFormatException: SPIN/JACKSON-JSON-01002 Expected 'String', got 'NULL'
    try {
        String firstName0 = jsonNode.jsonPath("$.name.first1").stringValue();
    } catch (Exception e) {
        // nothing ..
    }

    // work fine
    Object firstName1 = jsonNode.jsonPath("$.name.first1").element().value();
    Assert.assertEquals(firstName1, null);

    // work fine
    Object firstName2 = jsonNode.jsonPath("$.user.first1").element().value();
    Assert.assertEquals(firstName2, null);
}

Thank every reader!

1 Like