Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Jersey客户端POST方法提交数据_Java_Rest_Jersey - Fatal编程技术网

Java 如何使用Jersey客户端POST方法提交数据

Java 如何使用Jersey客户端POST方法提交数据,java,rest,jersey,Java,Rest,Jersey,我是从新泽西到新泽西的。我需要实现一个Jersey客户端,用POST方法提交数据。 curl命令是: curl -d '{"switch": "00:00:00:00:00:00:00:01", "name":"flow-mod-1", "priority":"32768", "ingress-port":"1","active":"true", "actions":"output=2"}' http://localhost:8080/wm/staticflowentrypusher/json

我是从新泽西到新泽西的。我需要实现一个Jersey客户端,用POST方法提交数据。 curl命令是:

curl -d '{"switch": "00:00:00:00:00:00:00:01", "name":"flow-mod-1", "priority":"32768", "ingress-port":"1","active":"true", "actions":"output=2"}' http://localhost:8080/wm/staticflowentrypusher/json
因此,我试图找出如何使用Jersey客户端实现上述curl命令

到目前为止,我已经做了:

public class FLClient {

private static Client client;
private static WebResource webResource;
private static String baseuri = "http://localhost:8080/wm/staticflowentrypusher/json";
private static ClientResponse response;
private static String output = null;

public static void main(String[] args) {
    try {

        client = Client.create();

        webResource = client.resource(baseuri);

                    // implement POST data 

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

有人能帮我吗?

现在我明白了。以下是我的解决方案:

public static void main(String[] args) {

    try {
        Client client = Client.create();

        WebResource webResource = client.resource(baseuri);

        String input = "{\"switch\": \"00:00:00:00:00:00:00:01\", "
                + "\"name\":\"flow-mod-1\", \"priority\":\"32768\", "
                + "\"ingress-port\":\"1\",\"active\":\"true\", "
                + "\"actions\":\"output=2\"}";

        // POST method
        ClientResponse response = webResource.accept("application/json")
                .type("application/json").post(ClientResponse.class, input);

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

        // display response
        String output = response.getEntity(String.class);
        System.out.println("Output from Server .... ");
        System.out.println(output + "\n");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

如果您想在JSON正文中发布,这里有一个更好的方法

ClientConfig clientConfig = new DefaultClientConfig();              
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);     
client = Client.create(clientConfig);

WebResource webResource = client.resource(baseuri);

Map<String,Object> postBody = new HashMap<String,Object>();
//put switch, name,priority....
ClientResponse response = webResource.accept("application/json")
                .type("application/json").post(ClientResponse.class, postBody);
ClientConfig ClientConfig=newdefaultclientconfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE\u POJO\u映射,Boolean.TRUE);
client=client.create(clientConfig);
WebResource=client.resource(baseuri);
Map postBody=newhashmap();
//输入开关、名称、优先级。。。。
ClientResponse response=webResource.accept(“应用程序/json”)
.type(“application/json”).post(ClientResponse.class,postBody);

请记住,您必须为未来的用户包括
jersey-json

,随着
jersey
的新版本,事情已经发生了变化,因此执行此类POST方法的方式是:

  • 对于版本
    1.x
    ():

  • 对于版本
    2.x

    WebTarget webTarget = client.target(baseuri);
    
    String input = "...";
    
    Response response = webTarget.request("application/json").post(Entity.json(input));
    

基于

我喜欢这种无JSON的JSON post方法。哈希映射。这听起来很酷,但我无法让它与FEATURE_POJO_MAPPING和jersey-json.jar:ClientHandlerException:找不到Java类型、类Java.util.HashMap和MIME媒体类型application/json的消息体编写器-我缺少什么?我不认为(WebTarget).accept()是2.x的一部分,至少不再是了。另外,我认为对于2.x来说,应该是响应,而不是ClientResponse。@Erhannis你在这两方面都是对的,我修正了答案。谢谢
WebTarget webTarget = client.target(baseuri);

String input = "...";

Response response = webTarget.request("application/json").post(Entity.json(input));