spring - 无法读取 HTTP 消息 : org. s.HttReadableException : Could not read document: Unrecognized token 'PUT' : was expecting ('true' , 'false' 或 'null')

标签 spring rest spring-data-jpa

我正在 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/

相关文章:

spring - 使用 Tomcat 使 Spring 与 3 层 Web 系统一起工作

java - 如何指定可执行 Spring Boot Jar 中存在的 WireMock 映射和文件的路径?

spring - 在 spring JDBC 批量更新中设置批量大小

javascript - 将多个对象传递给 angularjs $resource.save()

java - 仅当使用 spring-transactions 交易成功时如何发送电子邮件

spring - 使用带有 Hibernate Validator 的自定义 ResourceBundle

Restful 设计 : when to use sub-resources?

ruby-on-rails - 使用 Backbone.js 加载模型关系

java - Spring Data JPA 中的自定义(原始)查询

java - 使用 Spring Data JPA 的服务层中的 Crud 方法