Java代码: import javax.xml.bind.annotation.XmlRootElement;
// ContactDetails TO Object
@XmlRootElement
public class ContactDetails {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.rest.test.to;
import javax.xml.bind.annotation.XmlRootElement;
// PreviousEducationDetails TO Object
@XmlRootElement
public class PreviousEducationDetails {
private String qualification;
private String istitutionName;
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String getIstitutionName() {
return istitutionName;
}
public void setIstitutionName(String istitutionName) {
this.istitutionName = istitutionName;
}
}
// PersonalDetails TO Object
package com.rest.test.to;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement 公共类PersonalDetails {
private String firstName;
private String lastName;
private String address;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
// Javascript代码
$(document).ready(function() {
$("#studentInsert").click(function() {
alert("Reaches script !!");
var firstName = $("#firstName").val();
var lastName = $("#lastName").val();
var address = $("#address").val();
student.presonal_details = {
firstName : $("#firstName").val(),
lastName : $("#lastName").val(),
address : $("#address").val(),
}
student.contactPersonDetails=[{
name:"Name1",
address:"location1"
},{
name:"Name2",
address:"location2"
},{
name:"Name3",
address:"location3"
}]
student.previousEducationDetails=[
{qualification:"MCA",istitutionName:"Engineering College"},
{qualification:"MBA",istitutionName:"Engineering College"},
{qualification:"Msc",istitutionName:"Engineering College"}
]
$.ajax({
type : "post",
url : "http://localhost:8080/RestJersey/rest/jsonRequestReceiver/saveStudentDetail",
contentType: "application/json",
data : JSON.stringify(student),
success : function(data) {
console.log(data);
}
});
});
});
// Rest资源方法
package com.rest.test;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.rest.test.to.PersonalDetails;
import com.rest.test.to.PreviousEducationDetails;
import com.rest.test.to.StudentDetailsTO;
@Path("/jsonRequestReceiver")
public class JsonRequestReceiver {
@POST
@Path("/saveStudentDetail")
@Consumes(MediaType.APPLICATION_JSON)
public void saveStudentDetail(StudentDetailsTO student){
}
}
// StudentDetailsTO TO Object
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement 公共课StudentDetailsTO {
PersonalDetails personalDetails;
List<ContactDetails> contactDetails;
List<PreviousEducationDetails> previousEducationDetails;
public PersonalDetails getPersonalDetails() {
return personalDetails;
}
public void setPersonalDetails(PersonalDetails personalDetails) {
this.personalDetails = personalDetails;
}
public List<ContactDetails> getContactDetails() {
return contactDetails;
}
public void setContactDetails(List<ContactDetails> contactDetails) {
this.contactDetails = contactDetails;
}
public List<PreviousEducationDetails> getPreviousEducationDetails() {
return previousEducationDetails;
}
public void setPreviousEducationDetails(
List<PreviousEducationDetails> previousEducationDetails) {
this.previousEducationDetails = previousEducationDetails;
}
}
错误:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/RestJersey/rest/jsonRequestReceiver/saveStudentD etail
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:446
Content-Type:application/json
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/RestJersey/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{presonal_details: {firstName: "Murugesan", lastName: "Rathinam", address: "Karur"},…}
contactPersonDetails: [{name: "Name1", address: "location1"}, {name: "Name2", address: "location2"},…]
presonal_details: {firstName: "Murugesan", lastName: "Rathinam", address: "Karur"}
previousEducationDetails: [{qualification: "MCA", istitutionName: "Engineering College"},…]
Response Headersview source
Connection:close
Content-Type:text/plain
Date:Tue, 28 Apr 2015 06:41:56 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
ConsoleSearchEmulationRendering