RESTFul Web服务POST示例

时间:2015-12-24 20:35:57

标签: java web-services rest

您好我正在学习宁静的网络服务。我首先将一个单词发布到另一个Web应用程序并在第二个应用程序上显示该单词。

@Path("/hello")
public class HelloWorldService {`


    @Path("{f}")
      @GET
      @Produces("application/json")
      public Response PosttoInfo(@PathParam("f") String f) throws JSONException {

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("COMMAND", f); 


        String result = "\n\n POSTING to 2nd application : \n\n" + jsonObject;
        System.out.println(result);

        return Response.status(200).entity("\n\n\n\n POSTED ").build();



        Client client = Client.create();


        WebResource webResource2 = client.resource("http://localhost:8080/rest2/rest/hello/" +f);
        ClientResponse response2 = webResource2.get(ClientResponse.class);
        if (response2.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response2.getStatus());
        }

        String output2 = response2.getEntity(String.class);
        System.out.println("\n<<<============  POSTING command to 3rd application");
        System.out.println(output2);
}   
}

所以这里基本上只是将一个单词从一个应用程序发布到另一个应用程序。我收到错误说无法访问代码 客户端客户端= Client.create();

有人可以帮我解决这个错误吗?或者提供一些基本的示例代码,我们将单词或数据从一个应用程序发布到另一个应用程序

1 个答案:

答案 0 :(得分:0)

发布代码的格式有点奇怪,但是如果我看到它正确,编译器会告诉Config conf = new Config(); conf.setDebug(false); conf.put("topology.builtin.metrics.bucket.size.secs", 1); conf.registerMetricsConsumer(GraphiteMetricsConsumer.class, 4); conf.put("metrics.reporter.name", "com.verisign.storm.metrics.reporters.graphite.GraphiteReporter"); conf.put("metrics.graphite.host", "127.0.0.1"); conf.put("metrics.graphite.port", "2003"); conf.put("metrics.graphite.prefix", "storm.test"); 行由于它之前的Client client语句而无法访问。