我已经使用angular将对象数组发送到Api进行后期操作,该操作与3个对象的数组一起进行:
SLDto.java
public class SLDto {
private LetterDto letterDto;
private List<DocumentDto> documentDto;
private List<SelectionCustomOfficeDto> selectionCustomOfficeDto;
public SLDto() {
}
//i omitted getters and setters here
}
LetterDto.java
public class LetterDto {
private int clkletter;
private String inOut;
private String inOutNo;
private String inOutDate;
private String letterIssuedSubBy;
private String letterFile;
private String representativeName;
private int assessmentNo;
private int selectionNo;
public LetterDto() {
}
DocumentDto.java
public class DocumentDto {
private int docId;
private String docName;
private boolean checked;
public DocumentDto() {
}
}
SelectionCustomOfficeDto.java
public class SelectionCustomOfficeDto {
private int id;
private String fromDate;
private String toDate;
private int consignmentNo;
private int selectionId;
private int customOfficeId;
private String custOfficeName;
private String selectionName;
}
我需要将客户端对象映射到Api,所以我使用了方法:
@PostMapping(value = "/letter/create")
public String postAllOne(@RequestBody SLDto sldto ) {
//i tried 2ways to see the json data or trace it and assign into
respective objects but i am not getting.I tried
1st method
System.out.println(sldto.getLetterDto()); //Not working
2nd method
for(LetterDto letterDto:sldto.getLetterDto()) {
//it is not allowing me
}
return "success";
}
不允许我将其映射为:
如何将3json数据分为各自的对象?
[{"inOutNo":"2018-11-12","inOutDate":"2","inOut":"AnnexOne","letterFile":null,"representativeName":null,"assessmentNo":0,"letterIssuedSubBy":null,"selectionNo":8},[{"docId":1,"docName":"proforma invoice","checked":true},{"docId":2,"docName":"Packing list","checked":true}],[{"customOfficeId":"1","fromDate":"2018-11-12","toDate":"2018-11-20","consignmentNo":2,"selectionId":8,"selectionName":"PCS","custOfficeName":"Bhairawa Bhansar"}]]
看到的错误是
我遇到类似
的错误“ DefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误:无法反序列化的实例 com.ashwin.springsecurityangular.dto.SLDto脱离START_ARRAY令牌; 嵌套异常为 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法 反序列化com.ashwin.springsecurityangular.dto.SLD的实例到 START_ARRAY令牌”
答案 0 :(得分:1)
您应该像下面的json
这样发送:
{
"letterDto" : {"clkletter": "as" }, // your other properties,
"documentDto": [{"docId" : 1},{"docId" : 2}]// your other properties inside json object
"selectionCustomOfficeDto": [{"id": 12},{"id": 121}]// your other properties inside json object
}
以下是供您参考的示例,outer
类具有List<Inner2>
,List<Inner3>
和Inner1
Json
看起来像
{
"inner1": {"f1": "v1","f2": "v2"},
"inner2s": [{"f3": "v3","f4": "v4"},{"f3": "v5","f4": "v6"}],
"inner3s": [{"f5": "v7","f6": "v8"},{"f5": "v9","f6": "v10"}]
}
PoJo类
class Outer{
Inner1 inner1;
List<Inner2> inner2s;
List<Inner3> inner3s;
public Inner1 getInner1() {
return inner1;
}
public void setInner1(Inner1 inner1) {
this.inner1 = inner1;
}
public List<Inner2> getInner2s() {
return inner2s;
}
public void setInner2s(List<Inner2> inner2s) {
this.inner2s = inner2s;
}
public List<Inner3> getInner3s() {
return inner3s;
}
public void setInner3s(List<Inner3> inner3s) {
this.inner3s = inner3s;
}
}
class Inner1{
String f1;
String f2;
public String getF1() {
return f1;
}
public void setF1(String f1) {
this.f1 = f1;
}
public String getF2() {
return f2;
}
public void setF2(String f2) {
this.f2 = f2;
}
}
class Inner2{
String f3;
String f4;
public String getF3() {
return f3;
}
public void setF3(String f3) {
this.f3 = f3;
}
public String getF4() {
return f4;
}
public void setF4(String f4) {
this.f4 = f4;
}
}
class Inner3{
String f5;
String f6;
public String getF5() {
return f5;
}
public void setF5(String f5) {
this.f5 = f5;
}
public String getF6() {
return f6;
}
public void setF6(String f6) {
this.f6 = f6;
}
}
请求映射
@RequestMapping(value="/test",produces=MediaType.APPLICATION_JSON_VALUE,method= {RequestMethod.POST})
public String post(@RequestBody Outer outer) {
LOGGER.debug("Getting the logged in cutomer details" +outer);
Customer customer1 = new Customer("1", "customer1", "Sample@cust1.com");
LOGGER.info("The customer details are " + customer1);
return "done!!";
}
答案 1 :(得分:1)
您当前的POST
方法处理程序期望使用以下格式的JSON对象:
{
"letterDto" : {
"clkletter" : 1,
"inOut" : "Someting",
...
},
"documentDto" : [
{
"docId" : 1,
"docName" : "Name",
"checked" : true
} ,
{
"docId" : 2,
"docName" : "Name 2",
"checked" : false
}
],
"selectionCustomOfficeDto" : [
{
"id" : 1,
"fromDate" : "someDate,
...
},
{
"id" : 2,
"fromDate" : "someDate2,
...
}
]
}
但是目前,您正在发送JSON数组:
[
"letterDto" : {<letter properties>},
[
{
<document properties>
},
{
<document properties>
}
],
[
{
<selection custom office properties>
}
]
]
解决此问题后,您将能够遍历slDto.getDocumentDto
和slDto.getSelectionCustomOfficeDto
,因为它们是slDto
对象中包含的唯一 collections ,因此:
slDto.getSelectionCustomOfficeDto.forEach(s -> doSomething(s));
slDto.getDocumentDto.forEach(d -> doSomething(d));
您可以像这样调用Letter
的方法:
slDto.getLetterDto.getId();
答案 2 :(得分:1)
嗨,请确保您的JSON请求如下:
{
"letterDto" : {
"clkletter" : 1,
"inOut" : "some-string-value",
"inOutNo" : "some-string-value",
"inOutDate" : "some-string-value",
"letterIssuedSubBy" : "some-string-value",
"letterFile" : "some-string-value",
"representativeName" : "some-string-value",
"assessmentNo" : 1,
"selectionNo" : 1
},
"documentDto" : [
{
"docId" : 1,
"docName" : "some-doc-name",
"checked" : true
},
{
"docId" : 2,
"docName" : "some-doc-name",
"checked" : true
},
{
"docId" : 3,
"docName" : "some-doc-name",
"checked" : true
}
],
"selectionCustomOfficeDto" : [
{
"id" : 1,
"fromDate" : "some-date",
"toDate" : "some-date",
"consignmentNo" : 1,
"selectionId" : 1,
"customOfficeId" : 1,
"custOfficeName" : "some-office-name",
"selectionName" : "some-selection-name"
},
{
"id" : 2,
"fromDate" : "some-date",
"toDate" : "some-date",
"consignmentNo" : 1,
"selectionId" : 1,
"customOfficeId" : 1,
"custOfficeName" : "some-office-name",
"selectionName" : "some-selection-name"
},
{
"id" : 3,
"fromDate" : "some-date",
"toDate" : "some-date",
"consignmentNo" : 1,
"selectionId" : 1,
"customOfficeId" : 1,
"custOfficeName" : "some-office-name",
"selectionName" : "some-selection-name"
}
]
}
此外,在代码块中,
for(LetterDto letterDto:sldto.getLetterDto()) {
//it is not allowing me
}
此处sldto.getLetterDto()
返回一个LetterDto
Object
而不是List
,因此不可能进行迭代。
希望有帮助!