HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' はサポートされていません]
私は Spring Boot を学習している途中で、アプリケーションに Spring セキュリティを追加しようとしています。ちなみに、新しい顧客エンティティを投稿しようとすると、送信するペイロードが「application/json」をサポートしていないことを指定する Postman からの投稿リクエストを送信すると、「HttpMediaTypeNotSupportedException」を受け取ります。
顧客およびユーザーエンティティ:
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Entity
@DiscriminatorValue(value = "Customer")
public class Customer extends User {
private String phone;
@Embedded
@Valid
private Address address;
@OneToMany(mappedBy = "customer")
@JsonManagedReference
private List<Shipment> shipmentList;
@OneToOne(mappedBy = "customer")
@JsonManagedReference
private Cart cart;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "user_type")
@EntityListeners(AuditingEntityListener.class)
public class User implements UserDetails, Principal {
@Id
@GeneratedValue
private Integer id;
private String firstname;
private String lastname;
@NotEmpty(message = "{customer.email.not.empty}")
@Email(message = "{customer.email.invalid}")
@Column(unique = true, updatable = false)
private String email;
private String password;
private boolean accountLocked;
private boolean enabled;
@Enumerated(EnumType.STRING)
private Roles role;
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
@Column(insertable = false, updatable = true)
private LocalDateTime lastModifiedDate;
@Override
public String getName() {
return email; //email is the unique identifier of the User
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(role.name()));
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return email;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return !accountLocked;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
private String fullName(){
return firstname + lastname;
}
}
顧客DTO:
public record CustomerDTO(
@NotEmpty(message = "{customer.name.not.empty}")
String firstname,
@NotEmpty(message = "{customer.name.not.empty}")
String lastname,
@NotEmpty(message = "{customer.email.not.empty}")
@Email
String email,
String phone,
Address address,
@Valid
Cart cart,
@Valid
List<Shipment> shipments
){}
CustomerService の createCustomer():
public CustomerDTO createCustomer(CustomerDTO customerDTO) throws CustomerAlreadyExistsException, CustomerCreateNotValidException {
//first validate the DTO being sent in
validator.validate(customerDTO, "Invalid fields of new customer") ;
//check if this customer already exists
if(repository.findByEmail(customerDTO.email()) == null){
Customer customer = mapper.customerDTOToCustomer(customerDTO);
repository.save(customer);
} else {
throw new CustomerAlreadyExistsException(environment.getProperty("service.customer.already.exists"));
}
return customerDTO;
}
CustomerController の createCustomer():
@PostMapping(value = "customers")
public ResponseEntity<?> createCustomer(@RequestBody CustomerDTO customerDTO) throws CustomerAlreadyExistsException, CustomerCreateNotValidException {
CustomerDTO createdCustomer = customerService.createCustomer(customerDTO);
return new ResponseEntity<>(createdCustomer, HttpStatus.CREATED);
}
「application/json」が Spring Boot が Jackson 幼い頃に使用する当然のコンテンツ タイプであることを理解しています。そのため、このエラーについては混乱しています。また、Postman での投稿リクエストのヘッダー内で、コンテンツ タイプがapplication.jsonであると指定しました。しかし、まだエラーが発生します。
また、CustomerDTO がコントローラーに送信しているペイロードと不一致である可能性があるかどうかも確認しましたが、すべてのフィールドが一致しているように見えるため、それも当てはまらないようです。
Spring Security をアプリケーションに追加する前に、新しい顧客を通常に投稿できました。 ただし、springframework.security.core の UserDetails インターフェイスを実装するカスタム ユーザー エンティティから継承できるように Customer Entity を変更した後、新しい顧客を投稿する際に問題が発生しました。継承が問題の原因ですか?
また、mapstruct を使用してエンティティから DTO への検討を自動的に生成していますが、これも問題の原因ですか?
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2553
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(193) 评论(0)