2016-12-29 134 views
1

我在春季實施SPRING數據JPA + Oracle STS ......在郵遞員我得到的獲取方法,但對於後響應200它is`sample應用程序並將無法讀取從DB我猜的數據並在下面給出錯誤 -無法讀取HTTP消息:org.s.HttReadableException:無法讀取文檔:無法識別的令牌'PUT':期待('true','false'或'null')

無法讀取HTTP消息: org.springframework.http.converter.HttpMessageNotReadableException: 無法讀取文檔:無法識別的令牌「PUT」:在[Source: [email protected]期待 ('true','false'或'null') 6E7;行:1,列:5];嵌套0​​異常是com.fasterxml.jackson.core.JsonParseException: 無法識別的令牌'PUT':期待('true','false'或'null') at [Source:[email protected];行: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"; 
} 

回答

1

該錯誤可能是由於你是給你的控制器的數據的格式。你的控制器方法期待JSON字符串。 例如在jQuery的情況下,JSON.stringify()爲您提供JSON字符串。 因此,我建議您在客戶端確認您將數據發送到此控制器的位置。

我在處理我的一個項目時遇到了類似的錯誤。我的客戶端是用python編寫的,本應該給我發送JSON字符串。所以,我不得不使用轉儲(),它的工作。