java - POST请求的AfterMapping问题 - SPRING
问题描述
我有一个名为 User 的实体,它与其他实体有多种关系。好吧,我为这个实体创建了一个 DTO,并使用 MapStruct 创建了一个 Mapper。获取请求工作正常,但在尝试发布帖子时返回 500。
用户实体:
@Entity(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private String email;
private Boolean admin;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Account> accounts;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Budget budget;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Transaction> transactions;
用户 DTO:
public class UserDto {
private Long id;
private String username;
private String password;
private String email;
private Boolean admin;
private List<AccountDto> accounts;
private BudgetDto budgetDto;
private List<TransactionDto> transactions;
public UserDto() {
}
public UserDto(String username, String password,
String email, Boolean admin, List<AccountDto> accounts, BudgetDto budgetDto, List<TransactionDto> transactions) {
this.username = username;
this.password = password;
this.email = email;
this.admin = admin;
this.accounts = accounts;
this.budgetDto = budgetDto;
this.transactions = transactions;
}
用户映射器:
@Mapper(componentModel = "spring")
public abstract class UserMapper {
@Autowired
EntityManager entityManager;
public abstract UserDto toDto(User user);
@Mapping(target="accounts", ignore=true)
@Mapping(target="budget", ignore=true)
@Mapping(target="transactions", ignore=true)
public abstract User toEntity(UserDto userDto);
@AfterMapping
void afterToEntity(@MappingTarget User user, UserDto userDto){
List<AccountDto> accountDtoList = userDto.getAccounts();
List<Long> accountIds = accountDtoList.stream().map(AccountDto::getId).collect(Collectors.toList());
List<Account> accounts = accountIds
.stream()
.map(id -> entityManager.find(Account.class, id))
.collect(Collectors.toList());
user.setAccounts(accounts);
Long id = userDto.getBudgetDto().getId();
Budget budget = entityManager.find(Budget.class, id);
user.setBudget(budget);
List<TransactionDto> transactionDtoList = userDto.getTransactions();
List<Long> transactionIds = transactionDtoList.stream().map(TransactionDto::getId).collect(Collectors.toList());
List<Transaction> transactions = transactionIds
.stream()
.map(t_id -> entityManager.find(Transaction.class, t_id))
.collect(Collectors.toList());
user.setTransactions(transactions);
}
}
用户控制器:
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:4200")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserMapper userMapper;
@GetMapping("/test")
public String hello(){
return "Hello";
}
@GetMapping("/list")
public List<UserDto> getAll() {
List<User> categories = userService.findAll();
return categories.stream()
.map(userMapper::toDto)
.collect(Collectors.toList());
}
@PostMapping("/create")
public UserDto createUser(@Valid @RequestBody UserDto userDto) {
User user = userMapper.toEntity(userDto);
User userResult = userService.save(user);
return userMapper.toDto(userResult);
}
我尝试通过以下方式发出发布请求:
{“管理员”:“真”,“电子邮件”:“x@gmail.com”,“密码”:“x”,“用户名”:“x”}
它返回:
{“时间戳”:“2019-06-02T14:15:51.825 + 0000”,“状态”:500,“错误”:“内部服务器错误”,“消息”:“没有可用消息”,“跟踪”:“ java.lang.NullPointerException
解决方案
推荐阅读
- javascript - 无法读取welcomeChannel.send 中未定义的属性“发送”
- css - 图像 Flexbox 样式在 Github Markdown 中不起作用
- c++ - 在 C++ 中的链表中间插入节点时出现段错误
- python - Matplotlib:TexLive 安装不起作用
- excel - 结果数组是Excel中两个数组的减法
- javascript - 即使使用 css 工具 grid-column-gap 计算器列间隙也不会改变
- julia - 如何在 Julia 图中制作科学的 y 刻度?
- google-bigquery - 我可以使用大查询导出数据语句并安排查询吗?
- c++ - 如何正确使用 LocalAlloc 和 LocalReAlloc
- reactjs - 基于角色的同一 React 路由中的不同组件