【问题标题】:Failed to read HTTP message: org.s.HttReadableException: Could not read document: Unrecognized token 'PUT': was expecting ('true', 'false' or 'null')无法读取 HTTP 消息:org.s.HttReadableException:无法读取文档:无法识别的令牌“PUT”:期待(“真”、“假”或“空”)
【发布时间】:2017-05-13 12:13:27
【问题描述】:

我正在 Spring STS 中实现 SPRING 数据 JPA+ Oracle ...它是 postman 中的示例应用程序,我得到 Get 方法的响应 200,但对于 post 和 put 无法从 DB 读取数据我猜想并给出以下错误 -

读取 HTTP 消息失败: org.springframework.http.converter.HttpMessageNotReadableException: 无法读取文档:无法识别的令牌“PUT”:期待 (“真”、“假”或“空”)在 [来源: java.io.PushbackInputStream@1f1076e7;行:1,列:5];嵌套的 例外是 com.fasterxml.jackson.core.JsonParseException: 无法识别的令牌“PUT”:期待(“真”、“假”或“空”) 在 [来源:java.io.PushbackInputStream@1f1076e7;行:1,列: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; }
}

控制器方法:

@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";
}

【问题讨论】:

    标签: spring rest spring-data-jpa


    【解决方案1】:

    错误可能是由于您提供给控制器的数据格式所致。您的控制器方法需要 JSON 字符串。 例如,对于 jQuery,JSON.stringify() 为您提供 JSON 字符串。 因此,我建议您在将数据发送到此控制器的客户端确认这一点。

    我在处理我的一个项目时遇到了类似的错误。我的客户端是用 python 编写的,它应该向我发送 JSON 字符串。所以,我不得不使用 dumps() 并且它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2021-12-31
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多