如何使用Java在Play框架中读取JSON文件

如何使用Java在Play框架中读取JSON文件,java,json,junit,playframework,couchbase,Java,Json,Junit,Playframework,Couchbase,我正在尝试从我的play应用程序中的test/resources包中读取JSON文件。我得到了com.couchbase.client.java.error.DocumentDoesNotExistException。我相信我的道路是不正确的,有人能建议如何走绝对的道路吗 public class AppControllerTest extends WithApplication { @Inject AppDaoServiceImpl appDaoServiceImpl;

我正在尝试从我的play应用程序中的
test/resources
包中读取
JSON
文件。我得到了
com.couchbase.client.java.error.DocumentDoesNotExistException
。我相信我的道路是不正确的,有人能建议如何走绝对的道路吗

public class AppControllerTest extends WithApplication {

    @Inject
    AppDaoServiceImpl appDaoServiceImpl;

    private CouchbaseEnvironment env;
    private static Cluster cluster = null;
    private static Bucket bucket = null;
    private String testResources = System.getProperty("java.class.path") + "/test/resources/";

    private static final ALogger logger = Logger.of(AppControllerTest.class);

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Override
    protected Application provideApplication() {
        return new GuiceApplicationBuilder().build();
    }

    @Before
    public void init() {
        env = DefaultCouchbaseEnvironment.create();
        cluster = CouchbaseCluster.create(env, "127.0.0.1:8091");
        bucket = cluster.openBucket("CLUSTER", "admin123");

        try {
            String docId = "ABEBV_common";
            File testResource = new File(testResources + "ABEBV_common.json");
            FileInputStream is = new FileInputStream(testResource);
            JsonNode testData = Json.parse(is);
            RawJsonDocument rawJsonDocument = RawJsonDocument.create(docId, testData.asText());
            bucket.upsert(rawJsonDocument);

        } catch (Exception e) {
        }

    }

    @Test
    public void testGenericData() {
        Http.RequestBuilder request = new Http.RequestBuilder().method(GET).uri("/app/ms/genericdata/ABEBV")
                .header("client_id", "chase");

        Result result = route(app, request);
        assertEquals(OK, result.status());
        assertEquals("application/json", result.contentType().get());
        assertTrue(contentAsString(result).contains("141-GYCVZY"));
    }

    @After
    public void deleteDocuments() {
        bucket.remove("ABEBV_common");
        bucket.close();
        cluster.disconnect();
    }

}

是的,您的路径不正确,System.getProperty(“java.class.path”)将返回jvm引用的所有java类路径,而不是使用“user.dir”


private String testResources=System.getProperty(“user.dir”)+“/test/resources/”

如果试图从类路径读取文件,请参阅