Reputation: 588
i have problem with my Rest Client. My Service
@POST
@Path("/post")
@Consumes("text/plain")
public Response getNumber(String a){
return Response.status(201).entity("Number is: "+a.toString()).build();
}
My Client
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){}
My path is: ProjectName/core/take/post After Debug on a Server in Eclipse i get a message:
type Status report, message Method Not Allowed, description The specified HTTP method is not allowed for the requested resource. Tomcat 8.0.9
Please help me :(
Upvotes: 1
Views: 9508
Reputation: 4476
I have included only the jars(all) provided by jersey 1.9
version into my library.
Here is the REST Service :
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();
}
}
And here is the rest client : 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) {
}
}}
Try this you should get the output.
Upvotes: 1