Jersey - WebResource类型中的方法accept(MediaType [])不适用于参数(String)

时间:2015-01-20 09:16:07

标签: java jersey jersey-client

我尝试做泽西客户端,但是我收到了错误。 这是我的代码:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import javax.ws.rs.core.MediaType;

public class Test {
    public static void main(String[] args) {
        // Create Client and Handle to web resources
        String BASE_URI = "http://localhost:8080/HelloWorldWebapp/resources";
        Client client = Client.create();
        WebResource webResource = client.resource(BASE_URI);

    // send a GET request with Accept header set to "text/plain"
    String response = webResource.path("hello").accept(MediaType.TEXT_PLAIN).get(String.class);
    System.out.println(response);

    // send GET request with a query parameter value for 'name'
    response = (String) webResource.path("hello").queryParam("name", "Pranabh").get(String.class);
    System.out.println(response);

    // send GET request to /hello without any query param
    response = (String) webResource.path("hello").get(String.class);
    System.out.println(response);

    // send GET request to /hello/{name}
    response = webResource.path("hello").path("Ranjita").accept(MediaType.TEXT_PLAIN).get(String.class);
    System.out.println(response);

    // send a GET request and get the response encapsulate in ClientResponse
    ClientResponse clientResponse = (ClientResponse) webResource.path("hello").get(ClientResponse.class);
    System.out.println(clientResponse.getEntity(String.class));
}

} 

排队

String response = webResource.path("hello").accept(MediaType.TEXT_PLAIN).get(String.class);

有错误: WebResource类型中的方法accept(MediaType [])不适用于参数(String)

可能是什么原因? 我该如何解决?

感谢

2 个答案:

答案 0 :(得分:1)

java编译器是旧编译器。 我更新它,现在一切正常。

感谢您的回复

答案 1 :(得分:0)

创建一个m对象并将其作为参数传递给accept:

MediaType[] m = new MediaType[]{MediaType.TEXT_HTML_TYPE, 
                        MediaType.APPLICATION_XML_TYPE, MediaType.TEXT_XML_TYPE};

String response = webResource.path("hello").accept(m).get(String.class);