I have a simple reactive web app to learn the WebClient
usage. This has just a single controller called PostController
which delegates the request to an external API, fetches the response from it and then sends it to the client after serializing it. Unfortunately I am getting:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.spring_webclient_lombok_demo.entity.Post and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
Here is my application code:
PostController.java:
import java.util.List;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.web.bind.annotation.GetMapping;
import .springframework.web.bind.annotation.PathVariable;
import .springframework.web.bind.annotation.RequestMapping;
import .springframework.web.bind.annotation.RestController;
import .springframework.web.reactive.function.client.WebClient;
import com.example.spring_webclient_lombok_demo.entity.Post;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/posts")
public class PostController {
@Autowired
private WebClient webClient;
@GetMapping("")
public Mono<List<Post>> getPosts() {
return webClient.get()
.uri("/posts")
.retrieve()
.bodyToFlux(Post.class)
.collectList();
}
@GetMapping("{postId}")
public Mono<Post> getPostById(@PathVariable int postId) {
return webClient.get()
.uri("/posts/{id}", postId)
.retrieve()
.bodyToMono(Post.class);
}
}
Post.java:
import java.util.Objects;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
public class Post {
private int userId;
private int id;
private String title;
private String body;
// public Post() {
//
// }
//
// public Post(int userId, int id, String title, String body) {
// this.userId = userId;
// this.id = id;
// this.title = title;
// this.body = body;
// }
//
// public int getUserId() {
// return userId;
// }
//
// public void setUserId(int userId) {
// this.userId = userId;
// }
//
// public int getId() {
// return id;
// }
//
// public void setId(int id) {
// this.id = id;
// }
//
// public String getTitle() {
// return title;
// }
//
// public void setTitle(String title) {
// this.title = title;
// }
//
// public String getBody() {
// return body;
// }
//
// public void setBody(String body) {
// this.body = body;
// }
//
// @Override
// public int hashCode() {
// return Objects.hash(body, id, title, userId);
// }
//
// @Override
// public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
// Post other = (Post) obj;
// return Objects.equals(body, other.body) && id == other.id && Objects.equals(title, other.title)
// && userId == other.userId;
// }
//
// @Override
// public String toString() {
// return "Post [userId=" + userId + ", id=" + id + ", title=" + title + ", body=" + body + "]";
// }
}
SpringWebclientLombokDemoApplication.java
import .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;
import .springframework.context.annotation.Bean;
import .springframework.web.reactive.function.client.WebClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@SpringBootApplication
public class SpringWebclientLombokDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringWebclientLombokDemoApplication.class, args);
}
@Bean
public WebClient getWebClient() {
return WebClient.builder()
.baseUrl(";) // Set base URL
.build();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
xsi:schemaLocation=".0.0 .0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-webclient-lombok-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-webclient-lombok-demo</name>
<description>Demo spring project for practicing Webclient and Lombok</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I uncomment the explicit getters, setters, no-arg constructor, all-arg constructor, etc. in Post.java and remove the lombok annotations then I get the correct response. Why lombok annotations are not working here?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744355469a4570209.html
评论列表(0条)