annswerg
annswerg

Reputation: 279

JSON parse error - wrong Data type while posting json

I have an entity User, which has two fields of Data type.

@Entity
@Table(name = "users", schema = "public")
public class User {

    @Id
    @Column(name = "user_id", updatable = false, nullable = false, unique = true)
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    private UUID id;
    @Column(name = "name")
    private String name;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
    @Column(name = "created_on")
    @JsonIgnore
    private Date createdOn;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
    @Column(name = "modified_on")
    @JsonIgnore
    private Date modifiedOn;

// construuctors, getter, setter
}

I have a Controller with has method save() which saves data about user to postgesql database.

@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService service;

    @PostMapping("/save")
    public void save(@RequestBody User user){
        service.save(user);
    }

}

I have a JSON, which I post to controller

{ "name":"Max", "createdOn":"2019-07-26 11:13:39", "modifiedOn":"2019-07-26 11:13:39" }

And at last during execution I get an error

"JSON parse error: Cannot deserialize value of type java.sql.Date from String \"2019-07-26 11:13:39\": not a valid representation (error: Failed to parse Date value '2019-07-26 11:13:39': Cannot parse date \"2019-07-26 11:13:39\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.sql.Date from String \"2019-07-26 11:13:39\": not a valid representation (error: Failed to parse Date value '2019-07-26 11:13:39': Cannot parse date \"2019-07-26 11:13:39\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))\n at [Source: (PushbackInputStream); line: 3, column: 13] (through reference chain: com.example.api.entity.User[\"createdOn\"])"

Upvotes: 1

Views: 900

Answers (1)

user11044402
user11044402

Reputation:

Try to add a T:

2019-07-26T11:13:39

Upvotes: 2

Related Questions