Reputation: 33
I got this error
2021-02-04 10:31:48.341 WARN 6496 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of
com.sun.org.apache.xpath.internal.operations.String
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[email protected]'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.sun.org.apache.xpath.internal.operations.String
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[email protected]') at [Source: (PushbackInputStream); line: 5, column: 13] (through reference chain: com.sample.springbootdemo.domain.UserDTO["email"])
my code is,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my domain is,
package com.sample.springbootdemo.domain;
import com.sun.org.apache.xpath.internal.operations.String;
import javax.persistence.*;
@Entity // This tells Hibernate to make a table out of this class
@Table(name = "user")
public class UserDTO {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "user_id" )
private Integer id;
@Column(name = "first_name",columnDefinition = "varchar(255)")
private String firstName;
@Column(name = "last_name",columnDefinition = "varchar(255)")
private String lastName;
@Column(name = "age")
private Integer age;
@Column(name = "email",columnDefinition = "varchar(255)")
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
** my controller is,**
package com.sample.springbootdemo.controllers;
import com.sample.springbootdemo.domain.UserDTO;
import com.sample.springbootdemo.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path="/user")
public class User {
@Autowired
private UserServices userServices;
@GetMapping("/all")
public List<UserDTO> allUsers(){
return userServices.findAllUsers();
}
@PostMapping(path = "/add")
public String addUser(@RequestBody UserDTO Userdata){
return userServices.saveUser(Userdata);
}
}
please help me to fix this error.
Upvotes: 0
Views: 226
Reputation: 347
"String" should come with java natively. Do not use the import "com.sun.org.apache.xpath.internal.operations.String" Remove that line from your UserDTO.
Upvotes: 1