0

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.

rishi
  • 2,564
  • 6
  • 25
  • 47
  • How are you submtting your POST? What response code do you receive? – Daniel Scott Sep 09 '14 at 21:17
  • Could you try first changing the signature of "createNewFBUser" to not take parameters (and remove the println of username)? See if it gets into that method in the debugger. – David M. Karr Sep 09 '14 at 21:31
  • @DanielScott I am submitting POST through the client. Response code is 406 now that I checked it after you mentioned – rishi Sep 10 '14 at 05:47
  • @DavidM.Karr I tried without any parameters. It still does not reach the method. Also just an update, I checked the response code and it says 406. – rishi Sep 10 '14 at 05:49

1 Answers1

0

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.

Community
  • 1
  • 1
Daniel Scott
  • 7,418
  • 5
  • 39
  • 58
  • Yes. After looking at the error code and some online links, what actually worked for me was replacing accept(MediaType.APPLICATION_FORM_URLENCODED) part from my client and just sending a post. But I am not sure if that is the right way to do it. How important is it to specify it? – rishi Sep 10 '14 at 06:48
  • If you control both the server and client, then it's really up to you. It's a feature so that if the server could send back several different types, it can use a type that the client is able to read. – Daniel Scott Sep 10 '14 at 08:04