The method map(Task, Class) is undefined for the type ModelMapper TaskController.java

huangapple go评论104阅读模式
英文:

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&lt;TaskDto&gt;) 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

&lt;dependency&gt;
	&lt;groupId&gt;org.modelmapper&lt;/groupId&gt;
	&lt;artifactId&gt;modelmapper&lt;/artifactId&gt;
    &lt;version&gt;2.3.5&lt;/version&gt;
&lt;/dependency&gt;

答案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

huangapple
  • 本文由 发表于 2020年10月6日 16:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定