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
带POST方法的RESTful Java客户端_Java_Rest - Fatal编程技术网

带POST方法的RESTful Java客户端

带POST方法的RESTful Java客户端,java,rest,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

我的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方法。 Tomcat 8.0.9


请帮助我:(

我只将
jersey 1.9
版本提供的JAR(全部)包含在我的库中

以下是休息服务:

import javax.ws.rs.Consumes;
导入javax.ws.rs.GET;
导入javax.ws.rs.POST;
导入javax.ws.rs.Path;
导入javax.ws.rs.products;
导入javax.ws.rs.core.MediaType;
导入javax.ws.rs.core.Response;
@路径(“/hello”)

下面是rest客户端:
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) {
    }
}}

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

请在类级别对你的路径进行注释。@path(/take)path在我看来很好在请求的路径
/RESTform/core/take/post
中什么是
core,然后呢?它是web.xml:)中的一个名称空间当我更改post以获取我的页面上的“编号是:”没有123。。。我不明白这一点,用POST方法我有“方法不允许”405,用GET方法我仍然没有123…你应该得到正确的结果,因为我已经得到了。如果没有,请详细说明您的环境以及您是如何尝试它的。我在web上的计算机上有代理,我使用的是Eclipse Luna最新版本,java 7 update 67 1.9。我能告诉你更多吗?:)
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) {
    }
}}