I am trying to learn Jersey. I am facing issues getting POST method to work. Here is what my code looks like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Aou</display-name>
<display-name>AouTest</display-name>
<servlet>
<servlet-name>Test Jersy Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.aou.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test Jersy Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I have created a user service as below:
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.aou.dao.UserDAO;
import com.aou.dto.User;
import com.aou.exceptions.DatabaseException;
import com.aou.hibernate.DAOFactory;
import com.aou.utils.AouLogger;
@Path("/user")
public class UserService {
@Path("register")
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String createNewFBUser(@FormParam("username") String username,
@Context HttpServletResponse servletResponse) throws IOException {
System.out.println("Received request successfully");
return username;
}
@Path("test")
@GET
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String test() {
return "<html> " + "<title>" + "Hello Aou" + "</title>" + "<body><h1>"
+ "Test Successful" + "</body></h1>" + "</html> ";
}
}
And I am using client like this:
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.representation.Form;
public class CreateUserClient {
public void createNewUser() {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client
.resource("http://localhost:8080/Aou/rest/user/register");
Form form = new Form();
form.add("username", "username");
ClientResponse response = service.accept(
MediaType.APPLICATION_FORM_URLENCODED).post(
ClientResponse.class, form);
System.out.println("Form response " + response.getEntity(String.class));
}
public static void main(String[] args) {
new CreateUserClient().createNewUser();
}
When I use http://localhost:8080/Aou/rest/user/test
, it gives me well formed html. But for some reason the post call never reaches the method. Server never prints out "Received request successfully". I also tried remote debugging and it never hits that method. Nor is there any exception thrown on client or server side. I am not sure what am I doing wrong.
An HTTP response code of 406 means that you're not sending the correct accept header in your request.
What is "406-Not Acceptable Response" in HTTP?
There should be a way for you to add headers when your client makes a request. You need to add the header:
Accept:text/html
which is the mimetype of the response that your server is sending.