我REST-Jersey在我的项目中使用.所有POST数据都以JSON格式发送,并在服务器端解组为相应的bean.像这样的东西:
向服务器发送请求:
$('a#sayHelloPost').click(function(event){
event.preventDefault();
var mangaData = {
title:'Bleach',
author:'Kubo Tite'
}
var formData=JSON.stringify(mangaData);
console.log(formData);
$.ajax({
url:'rest/cred/sayposthello',
type: 'POST',
data: formData,
dataType: 'json',
contentType:'application/json'
})
});
Run Code Online (Sandbox Code Playgroud)
有效载荷:
{"title":"Bleach","author":"Kubo Tite"}
Run Code Online (Sandbox Code Playgroud)
服务器端:
@POST
@Path("/sayposthello")
@Produces(MediaType.APPLICATION_JSON)
public Response sayPostHello(MangaBean mb){
System.out.println(mb);
return Response.status(200).build();
}
Run Code Online (Sandbox Code Playgroud)
MangaBean:
public class MangaBean {
private String title;
private String author;
@Override
public String toString() {
return "MangaBean [title=" + title + ", author=" + author + "]";
}
public String getTitle() {
return title; …Run Code Online (Sandbox Code Playgroud)