当前位置: 首页 > 知识库问答 >
问题:

考虑在配置中定义一个类型为“UserConverter”的bean

夹谷星剑
2023-03-14
***************************
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);
}

共有1个答案

宋昊然
2023-03-14

您不能注入一个没有任何实际实现的抽象类。即使没有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());
}
    null
 类似资料: