在带有RestController的SpringBoot应用程序中,使用Mapstruct和lombok将域类映射到DT



下面是我的主要应用程序类。

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.example.demo")
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}

控制器类。

@RestController
public class JobAppController {

@Autowired
JobApplicationRepository jobApplicationRepository;

//@Autowired
//private JobApplicationMapper mapper;

JobApplicationMapper mapper = Mappers.getMapper(JobApplicationMapper.class);

@GetMapping("/hello")
public String greeting()
{
return "hello Mr";
}

@PostMapping("/save")
public JobApplicationDto save(@RequestBody JobApplicationDto jobApplicationDto)
{
JobApplication jobApplication = mapper.toEntity(jobApplicationDto);
jobApplication = jobApplicationRepository.save(jobApplication);
return mapper.ToDTO(jobApplication);

}
}

映射器接口。

@Mapper
@Component
public interface JobApplicationMapper {
//JobApplicationMapper mapper = Mappers.getMapper(JobApplicationMapper.class);
//({ @Mapping(target="employeeId", source="entity.id"),@Mapping(target="employeeName", source="entity.name")})

JobApplicationDto  ToDTO(JobApplication jobApplication);
JobApplication toEntity(JobApplicationDto jobApplicationDto);
}

我的pom文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jobSearch</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</name>
<description>Demo project for Spring Boot/JobSearch</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Beta2</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

域类。

@Entity
@Table(name = "Job_Application")
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
public class JobApplication {



@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id()
private Integer id;

@Column(name = "requisition_id")
private String requisition_id;

@Column(name = "job_posting_source")
private String job_posting_source;

@Column(name = "applied_date")
private Date applied_date;

@Column(name = "company")
private String company;

@Column(name = "current_status")
private String current_status;

@Column(name = "position")
private String position;

@Column(name = "location")
private String location;
//getters and setters follows...

dto类。

/*@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder*/
public class JobApplicationDto {

private Integer id;
private String requisition_id;
private String job_posting_source;
private Date applied_date;
private String company;
private String current_status;
private String position;
private String location;
//getters and setters follows

当试图通过poster将data/dto(作为JSON(发布到我的数据库表中时,数据会作为空值插入

{
"requisition_id" : "1234",
"job_posting_source" : "indeed",
"applied_date" : "2020-08-15",
"company" : "softInc",
"current_status" : "under review",
"position" : "SE3",
"location" : "Himalayas"
}

插入后的响应类似

{
"id": null,
"requisition_id": null,
"job_posting_source": null,
"applied_date": null,
"company": null,
"current_status": null,
"position": null,
"location": null
}

没有错误消息。不确定我错过了什么。经过彻底的调试,我意识到映射器实现没有问题。然而,由于我使用";lombok";对于域和dto类,没有生成getter和setter。因此,我现在手动将它们添加到代码中,并按预期工作。

附言:我认为这是一个初学者,所以欢迎任何形式的讨论/建议。

我现在要编辑这个问题,因为lombok的问题是为什么没有生成getter/setter。

我猜Lombok缺少注释处理器。。

尝试:

<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>

看看这个例子。

请注意:Lombok/MapStruct有几个问题。两者都是注释处理器。Lombok生成getter/setter,MapStruct使用它们。时机至关重要。因此,MapStruct引入了SPI来表示生成准备就绪。

相关内容

  • 没有找到相关文章

最新更新