考虑在您的配置中定义类型为 'UserConverter' 的 bean

Consider defining a bean of type 'UserConverter' in your configuration

我不知道我的 spring 启动应用程序发生了什么,但现在我无法启动它,因为出现错误:

***************************
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()));
}
}

映射器class:

@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);
}

首先我尝试 运行 它没有 @Service 注释,然后用它注释,但我总是看到同样的错误。

您无法注入没有任何实际实现的抽象 class。即使没有 Spring,在 Java 中也是不可能的。那么你期待它能被注入吗?

我不明白你为什么需要注入那个 class。最好的解决方案是使用适当的转换器创建实用程序 class,例如:

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());
}

此外,您的代码对于 API:

有一些错误
  • 映射到资源应该像 @RequestMapping("/api/users") 这样的复数 -> 只有当你需要通过 id add @GetMapping("/{id}")
  • 操作时
  • 创建方法应该 return 201 - Create 状态码而不是 200
  • 如果 /api 应该对所有 API 通用,您可以在所有资源的配置文件中定义它。
    来自 applicatoin.yml 的片段:

    服务器: 小服务程序: 上下文路径:/api

MapStruct 解决方案的有用参考资料: