我在下面有这个查询,它返回对象“消耗”列表。我在JSON反序列化期间收到错误,请参阅下面的第二种方法。
@Query("select new com.dto.Consumption(DATE(date), "
+ "sum(consume), "
+ "avg(consume), "
+ "min(consume), "
+ "max(consume)) "
+ "from HorlyConsumption "
+ "where plate.id = :plateId "
+ "and DATE(date) >= :date "
+ "group by DATE(date) "
)
List<Consumption> findAllDailyConsumptionByPlatelId(@Param("plateId") Long plateId, @Param("date") Date date);
@GetMapping(path = "/api/plates/{plateId}/daily")
public ResponseEntity<?> allDailyConsumption(
@PathVariable(value = "plateId") String plateId) {
return ResponseEntity.ok(dailyConsumptionService
.getAllDailyConsumptionByPlateId(plateId, LocalDate.now()));
}
public class Consumption implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Date date;
private Long sum;
private Double average;
private Long min;
private Long max;
public Consumption() {
}
public Consumption(Date date, Long sum, Double average, Long min, Long max) {
this.date = date;
this.sum = sum;
this.average = average;
this.min = min;
this.max = max;
}
... gets and sets
}
这是例外
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.dto.Consumption] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.dto.Consumption` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.dto.Consumption` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:115)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:991)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:974)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:725)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:359)
at org.springframework.boot.test.web.client.TestRestTemplate.getForEntity(TestRestTemplate.java:271)
at com.controller.PlateControllerTest.testAllDailyConsumptionWithAValidPlate(PanelControllerTest.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.dto.Consumption` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.dto.Consumption` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:223)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:100)
... 39 more
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.dto.Consumption` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1092)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1454)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:185)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3072)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:235)
... 41 more
请问,有什么帮助吗?
返回集合:
@GetMapping(path = "/api/plates/{plateId}/daily")
public ResponseEntity<List<Consumption>> allDailyConsumption(
@PathVariable(value = "plateId") String plateId) {
return ResponseEntity
.ok(dailyConsumptionService.getAllDailyConsumptionByPlateId(plateId,
LocalDate.now()));
}
下面提到的是JSON字符串ResultString: 我想将相同的内容转换为A类的对象: B类代码为: 但是获取异常: JSONMappingException:无法从START_ARRAY标记反序列化java.util.LinkedHashMap实例
我需要向网络服务发送帖子请求,但它在异常后返回。 实际服务器响应如下: 我根据以下内容发送请求 我的代码
给定以下JSON文件: 我试图使用Jackson对象映射器访问JSON文件中的值。具体如下:
我尝试使用jacksonapi解析json,并提供以下详细信息。但我错了。请查找以下代码和堆栈跟踪的详细信息。 JSON 控制器类 下面这个类是我的bean类。PublisheData.java 发布数据列表.java 我收到以下错误。
问题内容: 我正在尝试将json请求解析为模型。我不知道这段代码有什么问题。json的语法看起来正确,并且在Java模型上也有注释。我不知道为什么会出现如下错误: Java模型: 杰森代码: 问题答案: 您已声明为单个对象,但是将其作为JSON文档中多个对象的数组返回。 您的模型当前将parameters节点定义为一个对象: 这意味着您的模型对象需要一个如下所示的JSON文档: 但是在JSON文档