java - 使用 POST 方法的 RESTful Java 客户端

标签 java rest

我的 Rest 客户端有问题。 我的服务

@POST
@Path("/post")
@Consumes("text/plain")
public Response getNumber(String a){
    return Response.status(201).entity("Number is: "+a.toString()).build();
}

我的客户

   try{
        URL url = new URL("http://localhost:8080/RESTform/core/take/post");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/plain");
        String input = "123";
        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();
        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));
        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        conn.disconnect();
    }
    catch(MalformedURLException e){}
    catch(IOException e){}  

我的路径是:ProjectName/core/take/post 在 Eclipse 中的服务器上调试后,我收到一条消息:

输入状态报告, 消息方法不允许, 描述 所请求的资源不允许指定的 HTTP 方法。 汤姆猫8.0.9

请帮助我:(

最佳答案

我仅将 jersey 1.9 版本提供的 jars(全部)包含到我的库中。

这是 REST 服务:

导入 javax.ws.rs.Consumes; 导入 javax.ws.rs.GET; 导入 javax.ws.rs.POST; 导入javax.ws.rs.Path; 导入 javax.ws.rs.Produces; 导入 javax.ws.rs.core.MediaType; 导入 javax.ws.rs.core.Response; @Path("/你好")

public class HelloWorld {

@POST
@Path("/post")
@Consumes("text/plain")
public Response getNumber(String a){
    return Response.status(201).entity("Number is: "+a.toString()).build();
}
}

这是其余的客户端:import java.io.BufferedReader; 导入java.io.IOException; 导入 java.io.InputStreamReader; 导入 java.io.OutputStream; 导入 java.net.HttpURLConnection; 导入 java.net.MalformedURLException; 导入java.net.URL;

public class Test {
public static void main(String[] args) {

    try {
        URL url = new URL("http://localhost:5050/REST-simple/rest-simple/hello/post");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/plain");
        String input = "123";
        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();
        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));
        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
}}

试试这个你应该得到输出。

关于java - 使用 POST 方法的 RESTful Java 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377566/

相关文章:

java - Log4j 2.0 - 将更多转换说明符作为一个元素填充在一起

java - 实现 N 个常量之间链接的最佳方式

java - Glassfish 4 服务上的 Jersey 实体数据过滤

python - 使用 REST 端点将数据迁移到 Cassandra

java - 合并列表+更新项目

java - 从 Azure IoT 接收文件上传通知时出错

java - 创建后如何使用StoredProcedure在数据库中设置存储过程的名称

rest - HTTP状态的兼容性

java - 如何使用 Jersey 获取完整的 REST 请求正文?

java - 编写数据库支持的 Web 服务的最快方法