jersey java client返回HTTP错误代码:406

时间:2012-08-10 14:19:13

标签: java rest jersey

我正在对Teamcity URI进行REST调用,以获取lastSuccessful构建号但获得406.如果我在Chrome的REST控制台中使用相同的URI,我将获得正确的字符串(这是最新的构建号

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class LastSuccessBuildNum {

    public static void main(String[] args) {

        try {

            Client client = Client.create();
            // client basic auth demonstration 
            client.addFilter(new HTTPBasicAuthFilter("username", "password"));

            WebResource webResource = client
                    .resource("http://localteamcity.com/teamcity/app/rest/buildTypes/id:bt26/builds/status:SUCCESS/number");
            ClientResponse response = webResource.accept("application/json")
                    .get(ClientResponse.class);

            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }

            String output = response.getEntity(String.class);

            System.out.println("Output from Server .... \n");
            System.out.println(output);
        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

标准输出:

java.lang.RuntimeException: Failed : HTTP error code : 406
    at LastSuccessBuildNum.main(LastSuccessBuildNum.java:22)

1 个答案:

答案 0 :(得分:4)

检查Chrome REST客户端中的转移的MIME类型,也许它不是json。 406表示服务器没有客户端接受的MIME类型:http://www.checkupdown.com/status/E406.html

是否有特定原因使用jersey客户端而不是Apache Http Components?