In my SpringBoot code When i make use of Get request for user/{id} is working fine but when use of the request user/modelname.
I'm getting the exception:
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long';
carController.java
@GetMapping("/user/{id}")
public Car getUser(@PathVariable Long id) {
return carRepository.findOne(id);
}
@GetMapping("/user/modelname")
public List<Car> searchUserByModel(@RequestBody String modelname) {
return carRepository.findByModelname(modelname);
}
carRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface CarRepository extends JpaRepository<Car, Long>{
public List<Car> findByModelname(String modelname);
}
car.java
@Entity
public class Car {
@Id
@GeneratedValue
private Long id;
private String modelname;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getModelname() {
return modelname;
}
public void setModelname(String modelname) {
this.modelname = modelname;
}
public Car(Long id,String modelname) {
this.id=id;
this.modelname = modelname;
}
@Override
public String toString() {
return "Car [id=" + id + ", modelname=" + modelname +"]";
}
public Car() {
}
}
Your request is not reaching to @GetMapping("/user/modelname")
. it is always going to @GetMapping("/user/{id}")
only because it satisfied the condition. Try changing the url to something else like this @GetMapping("/users/modelname")
and check ,it will hit definitely.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.