我在嵌入式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;
使用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;
}
}
}
主要课程:
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
我的问题是:我做错了什么?
答案 0 :(得分:1)
您的终端不期望application/json
。它期待application/x-www-form-urlencoded
。
@FormParam略显特别,因为它从MIME媒体类型的请求表示中提取信息&#34; application / x-www-form-urlencoded&#34; ...
所以,你应该使用:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "data=test data" http://localhost:8888/rest/test/post