泽西岛服务不接受POST电话

时间:2014-09-09 21:09:39

标签: rest jersey jersey-2.0 jersey-client

我正在努力学习泽西岛。我正面临着让POST方法工作的问题。这是我的代码:

    <?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>

我创建了一个用户服务,如下所示:

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> ";

    }

}

我正在使用这样的客户:

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();
    }

当我使用http://localhost:8080/Aou/rest/user/test时,它给了我很好的html。但由于某些原因,后调用永远不会到达方法。服务器永远不会打印出&#34;收到请求成功&#34;。我也尝试过远程调试,它永远不会遇到这种方法。客户端或服务器端也没有任何异常。我不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

HTTP响应代码406表示您未在请求中发送正确的接受标头。

What is "406-Not Acceptable Response" in HTTP?

当您的客户提出请求时,应该有一种方法可以添加标头。您需要添加标题:

Accept:text/html

这是您的服务器发送的响应的mimetype。