我正在 spring STS 中实现 SPRING data JPA+ Oracle...它是 postman 中的示例应用程序,我收到 Get 方法的响应 200,但对于 post 和 put 无法从 DB 读取数据,我猜并给出以下错误 -
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'PUT': was expecting ('true', 'false' or 'null') at [Source: java.io.PushbackInputStream@1f1076e7; line: 1, column: 5]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'PUT': was expecting ('true', 'false' or 'null') at [Source: java.io.PushbackInputStream@1f1076e7; line: 1, column: 5]
@Service
public class PersonService {
@Autowired
private PersonRepository personRepository;
public Object findAll() {
return personRepository.findAll();
}
public Person findById(Long id) {
return personRepository.findOne(id);
}
public Person save(Person person) {
return personRepository.save(person);
}
public Person delete(Person person) {
personRepository.delete(person);
return person;
}
public Person findByEmail(String email){ return null; }
}
Controller 方法:
@RequestMapping(value = "/all", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Hashtable<String, Person> gatAll() {
return personService.getAll();
}
@RequestMapping(value = "/update/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String updateUser(@RequestBody Person person, @PathVariable long id) {
try {
person.setId(id);
personService.save(person);
} catch (Exception ex) {
return "Error in Updating the user : " + ex.toString();
}
return "User successfully Updated";
}
最佳答案
错误可能是由于您提供给 Controller 的数据格式所致。您的 Controller 方法需要 JSON 字符串。 例如,对于 jQuery,JSON.stringify() 为您提供 JSON 字符串。 因此,我建议您在将数据发送到此 Controller 的客户端确认这一点。
我在处理我的一个项目时遇到过类似的错误。我的客户端是用 python 编写的,它应该向我发送 JSON 字符串。所以,我不得不使用 dumps() 并且它起作用了。
关于spring - 无法读取 HTTP 消息 : org. s.HttReadableException : Could not read document: Unrecognized token 'PUT' : was expecting ('true' , 'false' 或 'null'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41375137/