Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换为Java数组时发生Java Spring JSON解析错误_Java_Json_Spring_Rest_Parsing - Fatal编程技术网

转换为Java数组时发生Java Spring JSON解析错误

转换为Java数组时发生Java Spring JSON解析错误,java,json,spring,rest,parsing,Java,Json,Spring,Rest,Parsing,我一直在尝试通过REST服务发送Java对象列表,但每当我尝试将其转换回Java数组时,就会出现JSON解析错误: 2018-06-10 10:50:08.044 WARN 13680 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableE

我一直在尝试通过REST服务发送Java对象列表,但每当我尝试将其转换回Java数组时,就会出现JSON解析错误:

2018-06-10 10:50:08.044  WARN 13680 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : 
   Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: 
   JSON parse error: Can not deserialize instance of com.master.entity.Greeting[] out 
   of START_OBJECT token; nested exception is 
   com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize 
   instance of com.master.entity.Greeting[] out of START_OBJECT token
   at [Source: java.io.PushbackInputStream@1e32b2a; line: 1, column: 1]
这是问候课:

public class Greeting {

private final long id;
private final String content;

public Greeting() {
    id = 0;
    content = "something";
}

public Greeting(long id, String content) {
    this.id = id;
    this.content = content;
}

public long getId() {
    return id;
}

public String getContent() {
    return content;
}
}
其余响应方法:

@RequestMapping("/getlist")
public List<Greeting> getList(Model model) {
    List<Greeting> greetings = new ArrayList<>();
    greetings.add(new Greeting(1, "Something"));
    greetings.add(new Greeting(2, "Something"));
    greetings.add(new Greeting(3, "Something"));
    greetings.add(new Greeting(4, "Something"));
    return greetings;
}

任何帮助都将不胜感激。谢谢大家!

我按照raiyan的建议将URL更改为正确的URL,现在效果很好

您在控制器中的路由是“/getlist”,但您正在
restGet
方法中调用“/greeting”。你能证实你打的是正确的路线吗?那太尴尬了。我对单个对象也使用了这种方法,但忘记了更改URL。谢谢你的回答!我应该自己考虑一下。
public void restGet() {
    String url = "http://192.168.0.11:8080/greeting";
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    ResponseEntity<Greeting[]> responseEntity = restTemplate.getForEntity(url, Greeting[].class);
    Greeting[] greeting = responseEntity.getBody();
    for (int i = 0; i < greeting.length; i++) {
        System.out.println("\n id: " + greeting[i].getId());
    }
}
[{"id":1,"content":"Something"},{"id":2,"content":"Something"},{"id":3,"content":"Something"},{"id":4,"content":"Something"}]