gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:54:42 25 4
gpt4 key购买 nike

我不知道我的 Spring Boot 应用程序发生了什么,但现在由于错误而无法启动它:

***************************
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

Controller 代码:

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

首先我尝试在没有 @Service 注释的情况下运行它,然后使用 it 注释运行它,但我总是看到相同的错误。

最佳答案

您无法注入(inject)没有任何实际实现的抽象类。即使没有 Spring,这在 Java 中也是不可能的。那么你预计它可以被注入(inject)吗?

我不明白为什么你需要注入(inject)该类。最好的解决方案将使用适当的转换器创建一个实用程序类,例如:

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进行操作时添加@GetMapping("/{id}")
  • create 方法应返回 201 - Create 状态代码,而不是 200
  • 如果 /api 对于您的所有 API 应该是通用的,您可以在所有资源的配置文件中定义它。
    来自 applicatoin.yml 的片段:

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

MapStruct 解决方案的有用引用:

关于java - 考虑在您的配置中定义类型为 'UserConverter' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61610293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com