我无法让它工作(使用@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();
}
}这是基于spring-boot-2.6.2,它与lombok-1.18.22一起来的。我所犯的错误是
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 = "")所做的工作
我是不是漏掉了什么明显的东西?
如果需要更多的信息,请告诉我
谢谢
发布于 2022-01-06 16:13:52
我解决了这个问题。这是由lombok的旧版本(1.18.12)通过vscode-lombok-plugin安装在VSCode上造成的,@Jacksonized只能从1.18.14开始使用。
github-回购中也存在许多问题,但似乎不再维护它了。
最简单的方法就是
VSCode%userprofile%\vscode\extensions\gabrielbb.vscode-lombok-1.0.1\server,删除现有的lombok.jar,复制下载的lombok.jar并放置到目录中VSCodeF1或Ctrl + Shift + P并选择Java: Clean Java Language Server WorkspaceVSCode它现在应该起作用了
https://stackoverflow.com/questions/70597244
复制相似问题