***************************
APPLICATION FAILED TO START
***************************
Description:
Field userConverter in webapp.controllers.UserResourceController required a bean of type 'webapp.converter.UserConverter' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'webapp.converter.UserConverter' in your configuration.
Process finished with exit code 1
@RestController
@RequestMapping("/api/user")
public class UserResourceController {
@Autowired
private UserServiceImpl userService;
@Autowired
private UserConverter userConverter;
@PostMapping
public ResponseEntity<UserDto> addUser(@RequestBody UserDto userDto) {
userService.persist(userConverter.toUser(userDto));
return ResponseEntity.ok().body(userDto);
}
@GetMapping
public ResponseEntity<List<UserDto>> findAllUsers() {
return ResponseEntity.ok(userConverter.toUserDtos(userService.getAll()));
}
@PutMapping("/api/user/{id}")
public ResponseEntity<UserDto> updateUser(@PathVariable Long id, @RequestBody UserDto userDto) {
User user = userConverter.toUser(userDto);
user.setId(id);
userService.persist(user);
return ResponseEntity.ok().body(userDto);
}
@GetMapping("/api/user/{id}")
public ResponseEntity<UserDto> findUser (@PathVariable Long id) {
Optional<User> user = Optional.ofNullable(userService.getByKey(id));
return ResponseEntity.ok(userConverter.toUserDto(user.get()));
}
}
@Mapper(componentModel = "spring")
@Service
public abstract class UserConverter {
public abstract User toUser(UserDto userDto);
public abstract UserDto toUserDto(User user);
public abstract List<UserDto> toUserDtos(List<User> users);
}
您不能注入一个没有任何实际实现的抽象类。即使没有Spring,在Java中也是不可能的。你认为它能被注射吗?
我不明白你为什么要给那个班注入。最好的解决方案将使用适当的转换器创建一个实用程序类,如:
public final class UserConverter {
private UserConverter() {}
public static UserDTO toUserDTO(User employee) {
Department empDp = employee.getDepartment();
return UserDTO.builder()
.id(employee.getId())
.name(employee.getName())
.active(employee.getActive())
.departmentId(empDp.getId())
.departmentName(empDp.getName())
.build();
}
public static User toUser(UserDTO dto) {
Department dp = Department.builder()
.id(dto.getDepartmentId())
.name(dto.getDepartmentName())
.build();
return User.builder()
.id(dto.getId())
.name(dto.getName())
.active(dto.getActive())
.department(dp)
.build();
}
}
并从代码中将其作为静态方法调用:
@GetMapping("/{id}")
public ResponseEntity<UserDto> findUser (@PathVariable Long id) {
return userService.getByKey(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
当我启动我的spring-boot应用程序时,我有这样的消息: com.gisapp.services.impl.userservice中的字段userDAO需要类型为“com.gisapp.gisapp.dao.IUserdao”的bean,但找不到该bean。 注入点有以下注释:-@org.springframework.beans.factory.annotation.AutoWired(r
包名:com.sample 现在,当我把所有文件放在一个包下时,它工作得很好。但当我根据功能进行分配时,错误就会发生。我该如何解决这个问题。 按照下面的建议添加basepackages后,我收到的错误为
我是spring的初学者。所以现在我开始学习spring boot并构建这个简单的项目,但是当我运行它时,我得到了这样的错误“Field entityManager in sagala.rest.boot.remade.dao.EmployeeDaoImpl required a bean of type'javax.persistence.entityManager'that count fou
我对整个Spring的生态系统都是陌生的。我一直在学习一些教程,能够创建一个Spring Boot应用程序并执行crud操作。然后我开始把这个项目改成mybatis的标准。 我已经尝试了许多其他类似问题的答案,但到目前为止没有一个是有效的。 下面是问题陈述: 实现类实现为: 我的Mapper类如下所示: 我的Mapper.xml课是: 最后是我的控制器类: 我得到的错误是: 描述: com.cru