我的Rest Client有问题。 我的服务
@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中的服务器上调试之后,我收到一条消息:
输入状态报告, 消息方法不允许, description对于请求的资源,不允许使用指定的HTTP方法。 Tomcat 8.0.9
请帮帮我:(
答案 0 :(得分:1)
我只将jersey 1.9
版本提供的jar(全部)包含在我的库中。
这是REST服务:
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/hello")
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;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import 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) {
}
}}
试试这个你应该得到输出。