Jersey 的 POST 方法使用嵌入式码头

Jersey's POST method usage with embedded jetty

我在嵌入式 Jetty 上有简单的 REST 服务:

    <dependencies>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-server</artifactId>
         <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependencies>

package test;

Class 使用 REST 服务:

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/test")
public class TestRESTService {
    @Path("/get")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        return Response.ok(new SimplePOJO("test get ok")).build();
    }


    @Path("/post")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response post(@FormParam("data") String data) {
        return Response.ok("your data is: " + data).build();
    }

    public static class SimplePOJO {
        private String message;

        public SimplePOJO(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

    }
}

主要class:

package test;

import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHolder;

public class JettyStart {
    public static void main(String[] args) {
        final Server server = new Server(8888);

        final org.mortbay.jetty.servlet.Context ctxRest = new org.mortbay.jetty.servlet.Context(server, "/rest");
        final ServletHolder jerseyServletHolder = ctxRest.addServlet(ServletContainer.class, "/*");
        jerseyServletHolder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
                "com.sun.jersey.api.core.PackagesResourceConfig");
        jerseyServletHolder.setInitParameter("com.sun.jersey.config.property.packages",
                TestRESTService.class.getPackage().getName());
        jerseyServletHolder.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
        ctxRest.addServlet(jerseyServletHolder, "/*");

        try {
            server.start();
        } catch (Exception ignore) {
            server.destroy();
        }
    }
}

当我尝试通过 ajax 调用访问 http://localhost:8888/rest/test/post 时 - 它工作正常。

$.ajax({
    url : "http://localhost:8888/rest/test/post",
    type : "POST",
    async : false,
    data: {
        data: "test data"
    }
});

Object {readyState: 4, responseText: "your data is: test data", status: 200, statusText: "OK"}

但是当我使用 curl 时,我得到 null 作为数据 参数.

curl -X POST -H "Content-Type: application/json" -d "{\"data\":\"test data\"}" http://localhost:8888/rest/test/post

your data is: null

我的问题是:我做错了什么?

您的端点不期望 application/json。预计 application/x-www-form-urlencoded.

来自Jersey documentation

@FormParam is slightly special because it extracts information from a request representation that is of the MIME media type "application/x-www-form-urlencoded"...

因此,您应该使用:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "data=test data" http://localhost:8888/rest/test/post