英文:
The method map(Task, Class<TaskDto>) is undefined for the type ModelMapper TaskController.java
问题
我目前正在一个Springboot项目上工作,虽然我很忙,但我遇到了这个错误:
The method map(Task, Class<TaskDto>) is undefined for the type ModelMapper TaskController.java
我在互联网上搜索了它,但是找不到适合这个问题的解决方案。
我正在使用这个函数
TaskController
@Autowired
private ModelMapper modelMapper;
private TaskDto toDto(Task task) {
TaskDto taskDto = modelMapper.map(task, TaskDto.class);
return taskDto;
}
ModelMapper
public class ModelMapper {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
依赖
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
</dependency>
英文:
I'm currently working on a project in Springboot, and while I'm busy, I am getting this error:
The method map(Task, Class<TaskDto>) is undefined for the type ModelMapper TaskController.java
I searched it on the internet, but i can't find the solution that fits the problem.
I'm using this function
TaskController
@Autowired
private ModelMapper modelMapper;
private TaskDto toDto(Task task) {
TaskDto taskDto = modelMapper.map(task, TaskDto.class);
return taskDto;
}
ModelMapper
public class ModelMapper {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
Dependency
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.5</version>
</dependency>
答案1
得分: 1
以下是翻译好的内容:
你应该使用 ModelMapperConfig 来创建 ModelMapper 的 bean。类名不应该是 ModelMapper,因为你正在使用依赖项创建该类的 bean。
@Configuration
public class ModelMapperConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
希望这对你有帮助!
你可以参考这个网站获取更多信息:
https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application
英文:
You should use ModelMapperConfig to create bean of ModelMapper. Name of class should not be ModelMapper because you are creating a bean of that class using dependency.
@Configuration
public class ModelMapperConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
Hope this will work for you!
You can refer this site for the same.
https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论