我正在创建一个spring Boot应用程序,我在其中使用Restful Api并通过POSTMAN以JSON格式发送数据。我希望数据与我的模型类一起映射,即请求数据类,但我得到空值。
主要班级
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ContactApplication {
public static void main(String[] args) {
SpringApplication.run(ContactGEApplication.class, args);
}
}
控制器类 - 这个是控制器类。在这个类中,我们将使用/ rfs映射url import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@RestController
public class ContactController {
private static Log log = LogFactory.getLog(ContactController.class);
@RequestMapping(value = "/rfs", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ServiceResponse<String> serviceRequest(@RequestBody RequestData requestData) throws JsonProcessingException {
System.out.println(requestData);
System.out.println("M in controller");
return ServiceResponse.success();
}
}
请求Data.java-这是我的模型类。在此我定义了所有字段和getter以及setter
public class RequestData {
private static final long serialVersionUID = 1L;
private String EquipmentID;
private String Modality;
private String FacilityID;
private String CountryCode;
private String ISOCode;
private String ProblemType;
private String ProblemArea;
private String EquipmentStatus;
private String Name;
private String PhoneNumber;
private String Extension;
private String Description;
private String ShortDescription;
private String TimePeriod;
private String ServiceCode;
private String Locale;
private String RequestingApp;
private String ExamNumber;
private String SeriesNumber;
private String ImageNumber;
public String getEquipmentID() {
return EquipmentID;
}
public void setEquipmentID(String equipmentID) {
EquipmentID = equipmentID;
}
public String getModality() {
return Modality;
}
public void setModality(String modality) {
Modality = modality;
}
public String getFacilityID() {
return FacilityID;
}
public void setFacilityID(String facilityID) {
FacilityID = facilityID;
}
public String getCountryCode() {
return CountryCode;
}
public void setCountryCode(String countryCode) {
CountryCode = countryCode;
}
public String getISOCode() {
return ISOCode;
}
public void setISOCode(String iSOCode) {
ISOCode = iSOCode;
}
public String getProblemType() {
return ProblemType;
}
public void setProblemType(String problemType) {
ProblemType = problemType;
}
public String getProblemArea() {
return ProblemArea;
}
public void setProblemArea(String problemArea) {
ProblemArea = problemArea;
}
public String getEquipmentStatus() {
return EquipmentStatus;
}
public void setEquipmentStatus(String equipmentStatus) {
EquipmentStatus = equipmentStatus;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
PhoneNumber = phoneNumber;
}
public String getExtension() {
return Extension;
}
public void setExtension(String extension) {
Extension = extension;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getShortDescription() {
return ShortDescription;
}
public void setShortDescription(String shortDescription) {
ShortDescription = shortDescription;
}
public String getTimePeriod() {
return TimePeriod;
}
public void setTimePeriod(String timePeriod) {
TimePeriod = timePeriod;
}
public String getServiceCode() {
return ServiceCode;
}
public void setServiceCode(String serviceCode) {
ServiceCode = serviceCode;
}
public String getLocale() {
return Locale;
}
public void setLocale(String locale) {
Locale = locale;
}
public String getRequestingApp() {
return RequestingApp;
}
public void setRequestingApp(String requestingApp) {
RequestingApp = requestingApp;
}
public String getExamNumber() {
return ExamNumber;
}
public void setExamNumber(String examNumber) {
ExamNumber = examNumber;
}
public String getSeriesNumber() {
return SeriesNumber;
}
public void setSeriesNumber(String seriesNumber) {
SeriesNumber = seriesNumber;
}
public String getImageNumber() {
return ImageNumber;
}
public void setImageNumber(String imageNumber) {
ImageNumber = imageNumber;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "RequestData [EquipmentID=" + EquipmentID + ", Modality=" + Modality + ", FacilityID=" + FacilityID
+ ", CountryCode=" + CountryCode + ", ISOCode=" + ISOCode + ", ProblemType=" + ProblemType
+ ", ProblemArea=" + ProblemArea + ", EquipmentStatus=" + EquipmentStatus + ", Name=" + Name
+ ", PhoneNumber=" + PhoneNumber + ", Extension=" + Extension + ", Description=" + Description
+ ", ShortDescription=" + ShortDescription + ", TimePeriod=" + TimePeriod + ", ServiceCode="
+ ServiceCode + ", Locale=" + Locale + ", RequestingApp=" + RequestingApp + ", ExamNumber=" + ExamNumber
+ ", SeriesNumber=" + SeriesNumber + ", ImageNumber=" + ImageNumber + "]";
}
}
答案 0 :(得分:0)
问题似乎是RequestData中的字段全部大写。这似乎导致getter和反序列化器出现问题。如果您将字段更改为以小写字母开头,并且在JSON请求中使用匹配的小写字段,则会填充RequestData。
答案 1 :(得分:0)
在Rest中的方法之前添加@ResponseBody
@RestController
public class ContactGEController {
private static Log log = LogFactory.getLog(ContactGEController.class);
@RequestMapping(value = "/rfs", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ServiceResponse<String> serviceRequest(@RequestBody RequestData requestData) throws JsonProcessingException {
System.out.println(requestData);
System.out.println("M in controller");
return ServiceResponse.success();
}
}