首页 > 解决方案 > 如何运行单元测试我收到 415 错误,但以正确的格式传递 json?

问题描述

@Test
public void testAuthenticationForSubmitModelingRequest() {

    String url = "http://localhost:9081/prop/submitModellingRequest";
    String name = "admin";
    String password = "admin";
    String authString = name + ":" + password;
    byte[] authStringEnc =  Base64.getEncoder().encode(authString.getBytes());
    String input = "{\"modelingVersionID\":\"1007\",\n" + 
            "\"modelingRequestID\":\"75891\",\n" + 
            "\"cycleId\":\"20033\",\n" + 
            " \"imtId\":\"225\",\n" + 
            " \"submarketID\":\"0\",\n" + 
            "\"brandSubSubGroupID\":\"11\",\n" + 
            "\"modelStatus\":null,\n" + 
            "\"modelingTechStatus\":null,\n" + 
            "\"requestType\":null,\n" + 
            "\"targetType\":\"0\",\n" + 
            "\"targetTypeName\":null,\n" + 
            "\"serverId\":\"1\"}";
    String authStringEnc1 = new String(authStringEnc);
    System.out.println("Base64 encoded auth string: " + authStringEnc);
    Client restClient = Client.create();
    WebResource webResource = restClient.resource(url);
    ClientResponse resp = webResource.accept("application/json").header("Authorization", "Basic " + authStringEnc1)
            .post(ClientResponse.class,input);
    if (resp.getStatus() != 201) {
        System.err.println("Unable to connect to the server");
    }
    String output = resp.getEntity(String.class);
    System.out.println("response: " + output);

}

尝试运行单元测试时出现 415 错误,即使以正确的格式传递 json..试图在我的 rest api 上调用 post 方法请帮助

标签: javarestjersey-client

解决方案


推荐阅读