【发布时间】:2020-12-26 03:55:03
【问题描述】:
以下是我的主要应用程序类。
@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
当尝试通过邮递员将数据/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
}
没有错误信息。不知道我错过了什么。 经过彻底调试,我意识到映射器实现没有问题。但是,由于我对域和 dto 类使用“lombok”,因此没有生成 getter 和 setter。所以我现在在代码中手动添加了它们,现在可以按预期工作了。
PS:我认为自己是初学者,因此欢迎任何形式的讨论/建议。
我现在要编辑这个问题,因为 lombok 的问题在于为什么没有生成 getter/setter。
【问题讨论】:
标签: dto lombok spring-restcontroller mapstruct