ajay_t
ajay_t

Reputation: 2385

Unable to call rest web service from html form action attribute

I am trying to call rest web service written in java from html form

My web service code is

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

      @POST
      @Path("/hello")
      @Consumes(MediaType.TEXT_HTML)
      @Produces(MediaType.TEXT_HTML)
      public String hello( @FormParam("username") String name1) {
  return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello from helpdesk" + "</body></h1>" + "</html> ";
      }

}

And my html page is

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action= "http://localhost:8080/helpdesk/rest/hello" method="POST">
        Username: <input type="text" name="username">
        <p>
        <input type="submit" value="Submit">
    </form>

</body>
</html>

Here in the html form, i have called the web service ../rest/hello. I have referred example from http://www.vogella.com/articles/REST/

Can anybody please tell me how to do it?

Thank you

Upvotes: 1

Views: 12773

Answers (2)

Veera
Veera

Reputation: 1805

In your code change the name of the textbox to name. Also change the path above the method "@Path("/hello{name}")".

Try with this path and url in html."@Path("/test")"

URL : /helpdesk/rest/hello/test

hello - is root class finder

test - is method finder in the root class

Refer the link "http://www.mastertheboss.com/resteasy/resteasy-tutorial-part-two-web-parameters" for difference between path param and form param

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

I see multiple problems in your code. First one is the way you have put the mapping of your REST service path:

  @Path("/hello{name}")

I don't think you can concatenate your two path params, I assume it to be a typo and expect this mapping to be:

  @Path("/hello/{name}")

Second problem is with your html code. You are trying to send the name as a FORM param, which is good for POST requests and not for GET requests. GET request expects the params in the URL or path as you are expecting it to be in your REST service code.

Now you have two choices to correct the code. Either change your REST service code method to POST from GET. Or you can send the name as path param from your HTML to hit your service correctly and getting the parameter.

If you change the method to POST, you may have to change the parameter to FormParam instead of PathParam.

Upvotes: 1

Related Questions