使用 Lombok @Jacksonized 抛出 InvalidDefinitionException
Using Lombok @Jacksonized throws InvalidDefinitionException
我无法让它工作(使用 @Jacksonized
)
@Value
@Builder
@Jacksonized
public class Flight {
private String id;
private String destination;
private int flightTime;
private int flightDuration;
}
使用以下设置
@RestController
public class FlightController {
@PostMapping("/flights")
public Flight create(@RequestBody Flight flight) {
return Flight.builder()
.id(UUID.randomUUID().toString())
.destination(flight.getDestination())
.flightTime(flight.getFlightTime())
.flightDuration(flight.getFlightDuration())
.build();
}
}
这是基于 lombok-1.18.22
附带的 spring-boot-2.6.2
。我得到的错误是
2022-01-06 01:13:01.899 DEBUG 14672 --- [nio-8080-exec-2] o.s.web.method.HandlerMethod : Could not resolve parameter [0] in public com.trial.lombokjackson.Flight com.trial.lombokjackson.FlightController.create(com.trial.lombokjackson.Flight): Type definition error: [simple type, class com.trial.lombokjackson.Flight]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]
2022-01-06 01:13:01.902 DEBUG 14672 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Failed to complete request: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.trial.lombokjackson.Flight]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]
2022-01-06 01:13:01.907 ERROR 14672 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.trial.lombokjackson.Flight]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]
// omitted the rest of the stack trace
但是,如果我将我的 pojo 更改为
@Value
@Builder
@JsonDeserialize(builder = Flight.FlightBuilder.class)
public class Flight {
private String id;
private String destination;
private int flightTime;
private int flightDuration;
@JsonPOJOBuilder(withPrefix = "")
public static class FlightBuilder { }
}
效果很好。根据我在各种网站和文档上的研究,@Jacksonized
应该取代我为 @JsonDeserialize(builder = Flight.FlightBuilder.class)
和 @JsonPOJOBuilder(withPrefix = "")
所做的
我是不是漏掉了什么明显的东西?
如果需要其他信息,请告诉我
谢谢
我想通了这个问题。这是由 lombok
(1.18.12) 的旧版本引起的,该版本与 VSCode
到 vscode-lombok-plugin 一起安装,并且 @Jacksonized
仅在 1.18.14
之后可用
github-repo 中也创建了多个问题,但似乎不再维护了。
最简单的方法是
- 退出
VSCode
- 从lombok
下载最新稳定版
- 导航到
%userprofile%\vscode\extensions\gabrielbb.vscode-lombok-1.0.1\server
,删除现有的lombok.jar
,复制下载的lombok.jar
并放入目录
- 开始
VSCode
- 按
F1
或Ctrl + Shift + P
和selectJava: Clean Java Language Server Workspace
- 出现提示时重新启动
VSCode
现在应该可以了
我无法让它工作(使用 @Jacksonized
)
@Value
@Builder
@Jacksonized
public class Flight {
private String id;
private String destination;
private int flightTime;
private int flightDuration;
}
使用以下设置
@RestController
public class FlightController {
@PostMapping("/flights")
public Flight create(@RequestBody Flight flight) {
return Flight.builder()
.id(UUID.randomUUID().toString())
.destination(flight.getDestination())
.flightTime(flight.getFlightTime())
.flightDuration(flight.getFlightDuration())
.build();
}
}
这是基于 lombok-1.18.22
附带的 spring-boot-2.6.2
。我得到的错误是
2022-01-06 01:13:01.899 DEBUG 14672 --- [nio-8080-exec-2] o.s.web.method.HandlerMethod : Could not resolve parameter [0] in public com.trial.lombokjackson.Flight com.trial.lombokjackson.FlightController.create(com.trial.lombokjackson.Flight): Type definition error: [simple type, class com.trial.lombokjackson.Flight]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]
2022-01-06 01:13:01.902 DEBUG 14672 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Failed to complete request: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.trial.lombokjackson.Flight]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]
2022-01-06 01:13:01.907 ERROR 14672 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.trial.lombokjackson.Flight]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.trial.lombokjackson.Flight` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]
// omitted the rest of the stack trace
但是,如果我将我的 pojo 更改为
@Value
@Builder
@JsonDeserialize(builder = Flight.FlightBuilder.class)
public class Flight {
private String id;
private String destination;
private int flightTime;
private int flightDuration;
@JsonPOJOBuilder(withPrefix = "")
public static class FlightBuilder { }
}
效果很好。根据我在各种网站和文档上的研究,@Jacksonized
应该取代我为 @JsonDeserialize(builder = Flight.FlightBuilder.class)
和 @JsonPOJOBuilder(withPrefix = "")
我是不是漏掉了什么明显的东西?
如果需要其他信息,请告诉我
谢谢
我想通了这个问题。这是由 lombok
(1.18.12) 的旧版本引起的,该版本与 VSCode
到 vscode-lombok-plugin 一起安装,并且 @Jacksonized
仅在 1.18.14
之后可用
github-repo 中也创建了多个问题,但似乎不再维护了。
最简单的方法是
- 退出
VSCode
- 从lombok 下载最新稳定版
- 导航到
%userprofile%\vscode\extensions\gabrielbb.vscode-lombok-1.0.1\server
,删除现有的lombok.jar
,复制下载的lombok.jar
并放入目录 - 开始
VSCode
- 按
F1
或Ctrl + Shift + P
和selectJava: Clean Java Language Server Workspace
- 出现提示时重新启动
VSCode
现在应该可以了