无法读取 HTTP 消息:org.s.HttReadableException:无法读取文档:无法识别的令牌“PUT”:正在等待(“true”、“false”或“null”)

Arv*_*rma 3 rest spring spring-data-jpa

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

无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法识别的令牌“PUT”:在 [来源:java.io. PushbackInputStream@1f1076e7; 行:1,列:5];嵌套异常是 com.fasterxml.jackson.core.JsonParseException:无法识别的标记“PUT”:在 [来源:java.io.PushbackInputStream@1f1076e7; 处期待(“true”、“false”或“null”);行: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; }
}
Run Code Online (Sandbox Code Playgroud)

控制器方法:

@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";
}
Run Code Online (Sandbox Code Playgroud)

小智 5

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

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