I am trying to run the localhost url for POST in jax -rs but every time I am trying to run the localhost I am not getting any results. For GET it is perfectly working.
#Path("playlists")
public class PlaylistResource implements PlaylistApi {
#Override
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response createPlaylist(PlaylistRequest request)
}
I tried:
localhost:9999/playlists/
If you want do a post request,you must use Postman or some other tools like that,if you only type into the address into a browser,it will be a get request always.
Related
I am working on (Maven Project) REST with Java (JAX-RS) using Jersey. I am trying to delete a Module according to the passed id
#DELETE
#Path("delete/{id}")
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_JSON})
public Module deleteModuleById(#PathParam("id") Long id) {
return repository.delete(id);
}
I am getting 405 - Method not allowed from tomcat server, not sure what am I doing wrong.
This is the Delete Method:
public Module delete(long id) {
EntityManager em = EM_FACTORY.createEntityManager();
em.getTransaction().begin();
Module m = em.find(Module.class, id);
if (m != null) {
em.remove(m);
} else {
throw new IllegalArgumentException("Provided id " + id + " does not exist!");
}
em.getTransaction().commit();
em.close();
return m;
}
Postman request for all Module:
Postman request for delete module with id=1:
Project Structure:
Your code seems to be ok. Check your Sending method. Please take into account that IllegalArgumentException will probably lead to 500 - Server error
Check via Curl
curl -X DELETE <YOUR HOST>/delete/123
Or check via any external resources like https://reqbin.com/, postman, etc.
As your code seems fine and you haven't added your postman request, I assume you may have set the wrong method type in your request. you set your request like this image below:
Please, replace base_url and your_id with your actual values
N.B: check the DELETE method I have set on left of the URL
#Consumes({MediaType.APPLICATION_JSON})
Postman automatically attaches the Content-Type header according to the settings of your request's body.
Your requests are set to HTML, not JSON.
It should give a different error, but this should cause an issue here.
405 Method not allowed occurs when you try to POST while the method is GET, for example.
In the postman requests I don't see you putting the param id. So, the call you are making will look like /api/modules/delete while it should have been /api/modules/delete/1. And if there is a method like api/modules/{x}, it will call this method finally creating the 405.
I just want to create a simple REST service and it uses #GET and #POST.
for the #GET function, everything is ok but for #POST, when I want to create a new user on my server the browser just keeps sating (METHOD NOT ALLOWED).
I read so many articles about how to fix this error but I haven't got anything yet.
My code for #POST :
#Path("/hello")
public class HelloResource(){
#POST
#Produces(MediaType.APPLICATION_JSON)
#Path("/post")
public Response createUser(#PathParam("name") String name,#PathParam("address") String address,#PathParam("birthYear") String birth,#PathParam("ps") String password) throws NotAllowedException,MethodNotFoundException,Exception {
DataStore.getInstance().putPerson(new Person(name, address, Integer.parseInt(birth), password));
String json = "{\n";
json += "\"status\": " + '"'+"CREATED" +'"'+ ",\n";
json+="}";
return Response.status(200).entity(json).build();
}}
I also tried adding #Consumes function with (MediaType.APPLICATION.JSON) and (MediaType.TEXT_PLAIN) but nothing changed.
Also the URL I enter for posting is :
http://localhost:8080/HelloREST/rest/hello/post?name=PouYad&address=mustbejsonlater&birthYear=2005&ps=12345
As you see I also tried so many exception handlers.
Can someone please help?
if you enter your URL in the browser URL address field, it won't work because the browser will send a "GET" request. So you must use a client that will allow you to send a "POST" like PostMan. Or write your own small httpConnection function that sends a "POST"
You also have to change the #PathParam to #FormParam for it to work (#QueryParam will also work, but because it is POST, it is best to use #FormParm).
Access URL directly through browser can only create Get Request, not POST Request
You should
Create HTML Form, set the action to your service url with POST method, and then submit it.
Use Rest Client like postman to access your service with POST method.
Write your own Http Client using java.net.http api or just simply use
one of the handy libraries/frameworks (Like Spring has RestTemplate).
Pardon for the amateur question, however, I am struggling with testing a java Rest Api Locally.
#Path("/Product") //URL to call
public class ProductSearch {
#Path("/item")
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces({MediaType.APPLICATION_JSON})
public List<ProductObject> getProjects(Credentials login) throws ConnectionException{
Assuming credentials has username, password, url, itemname, What should the localhost url look like? I get a 404 when I go to http://localhost:8080/productsearchapi/Product/item
I am able to deploy to heroku and test by sending json string but I need to be able to test and debug locally.
You could use postman to test your POST call to the API without deploying to heroku.
Your request in postman would look similar to the following, and under the body tab you would have to create the valid JSON that your path is consuming.
Postman Call
After hitting send with providing a valid JSON you should get the response json returned to you
I have an jax-rs endpoint as below. I need to post a message to a web page through this endpoint. When I execute the endpoint using a client the method with #GET executes. But the method with #POST does not execute. I need to know when will be the #POST method will execute. What should I do to invoke the #POST method.
#GET
#Path("/")
#Produces("text/plain")
public boolean getLoginStatus(#Context HttpServletRequest request) throws URISyntaxException {
return true;
}
#POST
#Path("/")
public boolean helloPost() {
return true;
}
You need to invoke a HTTP POST request from your client - be it a programmatic one (e.g. JAX-RS 2.0 client API), a browser or tools like curl etc. I would strongly suggest using Postman client as a chrome browser extension to execute a POST request and test out your REST service
I have a web service with 3 endpoints. as follows -
GET /Game/getGameAll/ (com.service.rest.Game)
GET /Game/getGameById/{gameId} (com.service.rest.Game)
POST /Game/updateGame/{gameId}/{isAvailable} (com.service.rest.Game)
For testing I use -
localhost:8080/Game/getGameAll/
localhost:8080/Game/getGameById/1000
and it works perfectly fine.
but when executing update functionality -
localhost:8080/Game/updateGame/1000/true
it gives me an error 404: method not found.
But if i change the annotation from post to get. It executes.
//#POST : If this is changed to Get, it works! But not with #POST.
#GET
#Path(value = "/updateGame/{gameId}/{isAvailable}")
#Produces(MediaType.APPLICATION_JSON)
public Game updateGame(
#PathParam(value = "gameId") Integer gameId,
#PathParam(value = "isAvailable") int isAvailable) { ..
.
}
How can i execute the Post method of a webservice?
Are you trying this from your web browser? You won't be able to call POST methods that way.
You can either use curl from your command line or an interactive client such as Postman.