Home > Blockchain >  Content-Type 'application/json;charset=UTF-8' is not supported SpringBoot Rest
Content-Type 'application/json;charset=UTF-8' is not supported SpringBoot Rest

Time:01-10

I have this exception when I'm trying to send POST or PUT request on RestController enter image description here I don't use Jackson which solve all other similar problem on SOF. Controller: ` @RequestMapping("/api/v1/users") @RestController @RequiredArgsConstructor @Validated public class UserController {

private final UserService userService;

@PutMapping
@Validated(UserDto.OnUpdate.class)
public void save(@Valid @RequestBody UserDto userDto) {
    User user = UserMapper.INSTANCE.toEntity(userDto);
    userService.save(user);
}

@PostMapping
@Validated(UserDto.OnCreate.class)
public UserDto create(@Valid @RequestBody UserDto userDto) {
    User userToBeCreated = UserMapper.INSTANCE.toEntity(userDto);
    User user = userService.create(userToBeCreated);
    return UserMapper.INSTANCE.toDto(user);
}
}`

UserDto: ` @Data @JsonIgnoreProperties({"password", "passwordConfirmation"}) public class UserDto {

@Null(message = "Id must be null", groups = {OnCreate.class})
@NotNull(message = "Id must not be null", groups = {OnUpdate.class})
private Long id;

@NotNull(message = "Name is required", groups = {OnCreate.class, OnUpdate.class})
@Length(min = 3, max = 45, message = "Username must be between {min} and {max} characters", groups = {OnCreate.class, OnUpdate.class})
private String name;

@NotNull(message = "Email is required", groups = {OnCreate.class, OnUpdate.class})
@Length(min = 3, max = 45, message = "Email must be between {min} and {max} characters", groups = {OnCreate.class, OnUpdate.class})
@Email(message = "Email must be valid")
private String email;

@NotNull(message = "Phone number is required", groups = {OnCreate.class, OnUpdate.class})
@Length(min = 3, max = 45, message = "Phone number must be between {min} and {max} characters", groups = {OnCreate.class, OnUpdate.class})
private String phoneNumber;

@NotNull(message = "Password is required", groups = {OnCreate.class, OnUpdate.class})
@Length(min = 6, message = "Password must be longer than {min} characters", groups = {OnCreate.class, OnUpdate.class})
private String password;

@NotNull(message = "Password confirmation is required", groups = {OnCreate.class})
@Length(min = 6, message = "Password confirmation must be longer than {min} characters",    groups = {OnCreate.class})
private String passwordConfirmation;

private List<AddressDto> addresses;

private List<OrderDto> orders;

private CartDto cart;

private LocalDate createdAt;

public interface OnCreate {
}

public interface OnUpdate {
}
}`

User: ` @Data public class User {

private Long id;
private String name;
private String email;
private String password;
private String passwordConfirmation;
private String phoneNumber;
private List<Address> addresses;
private List<Order> orders;
private Cart cart;
private LocalDateTime createdAt;
}`

I have similar requests which is working, but difference in User has other objects inside, so I think problem is in it.

I've tried to debug and remove validation annotations, but this request does not go into method.

As I mentioned, @PostMapping(consumes = "application/json;charset=UTF-8") does not work. I have this exception because I have refs on other classes, but they don't need to be validated, but If i remove code with another classes it works

CodePudding user response:

Spring Boot definitely supports Content-Type 'application/json;charset=UTF-8'. Just chenge your annotation @PostMapping to @PostMapping(consumes = "application/json;charset=UTF-8") See Spring @GetMapping and @PostMapping article for detailed explanation with examples

CodePudding user response:

Solution is in @JsonProperty(access = JsonProperty.Access.READ_ONLY) annotation above fields of other classes. It makes to avoid validation when I send a request to server

  • Related