java - 使用 Spring REST 和 Jackson 解析以下 JSON 时获取 "The request sent by the client was syntactically incorrect"

标签 java json spring rest spring-mvc

使用 Spring 4 和 Jackson 1.9.13,在进行其余调用时,我收到一条错误消息,提示“客户端发送的请求在语法上不正确”,并且调用不会进入 REST 方法...

JSON 是数组的数组格式:

[ "location",
  [
    {
      "regions":[],
      "x":10.0,
      "y":14.0,
      "timeExpires":1387219731911,
      "confidence":0.9,
      "timestamp":1387219671911,
      "source":"wifi",
      "associated":true,
      "clientMac":"1c:4f:2b:14:c9:8b",
      "clientType":"AbC Device",
      "adspNetworkPath":"/a/b/c1",
      "folderID":10007,
      "floorNumber":1,
      "timeComputed":1387219671911
    },
    {
      "regions":[],
      "x":8.222222,
      "y":18.88889,
      "timeExpires":1387219726912,
      "confidence":0.9,
      "timestamp":1387219666912,
      "source":"wifi",
      "associated":true,
      "clientMac":"64:a3:ab:6b:5d:4f",
      "clientType":"123 Device",
      "adspNetworkPath":"/a/b/d1",
      "folderID":10007,
      "floorNumber":1,
      "timeComputed":1387219666912
    }
  ]
]

Spring Controller 看起来像:

@RequestMapping(value="/location", method=RequestMethod.POST)
  public @ResponseBody void locationData(@RequestBody List<Location> locationList) {
    logger.info("Inside locationData() method...");

    ObjectMapper mapper = new ObjectMapper();
    try {
        // just convert to list of object and display back for now...           
        String jsonString = mapper.writeValueAsString(locationList);
        logger.info(jsonString);
    } catch (IOException e) {
        e.printStackTrace();
    }
  }

模型对象是我遇到问题的地方。

package com.tester;

import java.util.List;

import org.codehaus.jackson.annotate.JsonProperty;

public class Location {
    private String location;
    private List<AdspData> locationArray;

    @JsonProperty(value = "location")
    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @JsonProperty(value = "location-array")
    public List<AdspData> getLocationArray() {
        return locationArray;
    }

    public void setLocationArray(List<Data> locationArray) {
        this.locationArray = locationArray;
    }

    @Override
    public String toString() {
        return "Location [location=" + location + ", locationArray="
                + locationArray + "]";
    }
}

其他模型类:

package com.tester;

import java.util.ArrayList;
import java.util.Date;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.tester.util.JsonDateSerializer;

public class AdspData {
    private ArrayList<String> regions;
    private double xLoc;
    private double yLoc;
    private double confidence;
    private Date timeExpires;
    private Date timeStamp;
    private Date timeComputed;
    private String source;
    private String clientMac;
    private String clientType;
    private String adspNetworkPath;
    private int folderId;
    private int floorNumber;
    private boolean associated;

    public AdspData() {     
    }

    @JsonProperty(value = "regions")
    public ArrayList<String> getRegions() {
        return regions;
    }

    public void setRegions(ArrayList<String> regions) {
        this.regions = regions;
    }

    @JsonProperty(value = "x")
    public double getxLoc() {
        return xLoc;
    }

    public void setxLoc(double xLoc) {
        this.xLoc = xLoc;
    }

    @JsonProperty(value = "y")
    public double getyLoc() {
        return yLoc;
    }

    public void setyLoc(double yLoc) {
        this.yLoc = yLoc;
    }

    @JsonProperty(value = "confidence")
    public double getConfidence() {
        return confidence;
    }

    public void setConfidence(double confidence) {
        this.confidence = confidence;
    }

    @JsonProperty(value = "timeExpires")
    @JsonSerialize(using = JsonDateSerializer.class)
    public Date getTimeExpires() {
        return timeExpires;
    }

    public void setTimeExpires(Date timeExpires) {
        this.timeExpires = timeExpires;
    }

    @JsonProperty(value = "timestamp")
    @JsonSerialize(using = JsonDateSerializer.class)
    public Date getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Date timeStamp) {
        this.timeStamp = timeStamp;
    }

    @JsonProperty(value = "timeComputed") 
    @JsonSerialize(using = JsonDateSerializer.class)
    public Date getTimeComputed() {
        return timeComputed;
    }

    public void setTimeComputed(Date timeComputed) {
        this.timeComputed = timeComputed;
    }

    @JsonProperty(value = "source")
    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    @JsonProperty(value = "clientMac")
    public String getClientMac() {
        return clientMac;
    }

    public void setClientMac(String clientMac) {
        this.clientMac = clientMac;
    }

    @JsonProperty(value = "clientType")
    public String getClientType() {
        return clientType;
    }

    public void setClientType(String clientType) {
        this.clientType = clientType;
    }

    @JsonProperty(value = "adspNetworkPath")
    public String getAdspNetworkPath() {
        return adspNetworkPath;
    }

    public void setAdspNetworkPath(String adspNetworkPath) {
        this.adspNetworkPath = adspNetworkPath;
    }

    @JsonProperty(value = "folderID")
    public int getFolderId() {
        return folderId;
    }

    public void setFolderId(int folderId) {
        this.folderId = folderId;
    }

    @JsonProperty(value = "floorNumber")
    public int getFloorNumber() {
        return floorNumber;
    }

    public void setFloorNumber(int floorNumber) {
        this.floorNumber = floorNumber;
    }

    @JsonProperty(value = "associated")
    public boolean isAssociated() {
        return associated;
    }

    public void setAssociated(boolean associated) {
        this.associated = associated;
    }

    @Override
    public String toString() {
        return "AdspData [regions=" + regions + ", xLoc=" + xLoc
                + ", yLoc=" + yLoc + ", confidence=" + confidence
                + ", timeExpires=" + timeExpires + ", timeStamp=" + timeStamp
                + ", timeComputed=" + timeComputed + ", source=" + source
                + ", clientMac=" + clientMac + ", clientType=" + clientType
                + ", adspNetworkPath=" + adspNetworkPath + ", folderId="
                + folderId + ", floorNumber=" + floorNumber + ", associated="
                + associated + "]";
    }
}

最佳答案

应该是这样的。

[
  {
    "location": "Location Data",
    "locationArray": [
      {
        "regions": [],
        "x": 10.0,
        "y": 14.0,
        "timeExpires": 1387219731911,
        "confidence": 0.9,
        "timestamp": 1387219671911,
        "source": "wifi",
        "associated": true,
        "clientMac": "1c:4f:2b:14:c9:8b",
        "clientType": "AbC Device",
        "adspNetworkPath": "/a/b/c1",
        "folderID": 10007,
        "floorNumber": 1,
        "timeComputed": 1387219671911
      },
      {
        "regions": [],
        "x": 8.222222,
        "y": 18.88889,
        "timeExpires": 1387219726912,
        "confidence": 0.9,
        "timestamp": 1387219666912,
        "source": "wifi",
        "associated": true,
        "clientMac": "64:a3:ab:6b:5d:4f",
        "clientType": "123 Device",
        "adspNetworkPath": "/a/b/d1",
        "folderID": 10007,
        "floorNumber": 1,
        "timeComputed": 1387219666912
      }
    ]
  }
]

关于java - 使用 Spring REST 和 Jackson 解析以下 JSON 时获取 "The request sent by the client was syntactically incorrect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35041346/

相关文章:

java - 使用 Unitils 针对 Postgres 数据库运行单元测试时出现 NullPointerException

java - xhtml to pdf servlet with flyingsaucer

Java:在命令行中更新文本而无需换行

ios - 可以从 json 文件加载元组数组吗?

spring - 是否可以使用gradle构建vaadin 14 Spring项目?

java - Spring、PagingAndSortingRepository、findOne 和 @EmbeddedId

java - 将数据插入sqlite java表

java - 如何将图片添加到 javaFX 2.0 netbeans 项目

javascript - 无法为d3js强制布局网络可视化添加标签

javascript - Angular 将数据传递到模态和模态实例