Beacze Beacze - 1 month ago 6
reST (reStructuredText) Question

REST Service doesn't consume data from form

I have written two simple functions, GET and POST, for consume data from form. Unfortunately when I'm submitting this form, instead of get response, form with empty fields is again displayed. It changes URL address from http://localhost:8080/example to http://localhost:8080/example/?version=5.4&id=1. What is missing for displaying correct response?

@Component
@Path("/example")
public class ExampleService {
@GET
public String message() {
return "<!DOCTYPE html>\n" +
"<html>\n" +
"<body>\n" +
"\n" +
"<form>\n" +
" Version\n" +
" <input type=\"text\" name=\"version\" value=\"\">\n" +
" Id\n" +
" <input type=\"text\" name=\"id\" value=\"\">\n" +
" <input type=\"submit\">\n" +
"</form>\n" +
"\n" +
"</body>\n" +
"</html>";
}

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.TEXT_HTML})
public Response createFromForm(
@FormParam("version") String version,
@FormParam("id") String id
) {
return Response.status(201).entity(version).build();
}
}


Edit. One more additional question. How I can integrate index.html file in this project? I have created webapp folder and places inside it index.html file (strucuture of folders below)?
enter image description here

Answer

Looks like your form is submitting the results as a url (GET).

add method="POST" to your form

ie:

<form method="POST">
// other parts here
</form>