【发布时间】:2019-01-17 09:47:14
【问题描述】:
我在使用 RestTemplate 从另一个服务获取数据时遇到了邮递员错误。
我有两个服务。第一个服务有 userdetails 实体类,第二个服务有帖子实体类。这是后期控制器的代码。
@GetMapping("/findpostbyareacode/{areacode}")
List<Posts> getPostByAreacode(@PathVariable(value="areacode") Long areacode){
RestTemplate restTemplate=new RestTemplate();
ResponseEntity<List<UserDetails>> response=
restTemplate.exchange("http://localhost:8091/voiceofbengal/userdetailsbyareacode/"+areacode, HttpMethod.GET,
null, new ParameterizedTypeReference<List<UserDetails>>(){
});
List<UserDetails> body=response.getBody();
List<Posts> postList=new ArrayList<>();
List<Posts> totalPost=new ArrayList<>();
for(UserDetails users : body) {
totalPost=postService.findByU_id(users.getU_id());
for(Posts posts : totalPost) {
postList.add(posts);
}
}
return postList;
}
我在邮递员中收到此错误。
"timestamp": "2019-01-17T09:10:32.977+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Error while extracting response for type [java.util.List<com.css.vob.model.UserDetails>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String \"01-01-1990\": not a valid representation (error: Failed to parse Date value '01-01-1990': Cannot parse date \"01-01-1990\": not compatible with any of standard forms (\"yyyy-MM-dd'T'HH:mm:ss.SSSZ\", \"yyyy-MM-dd'T'HH:mm:ss.SSS\", \"EEE, dd MMM yyyy HH:mm:ss zzz\", \"yyyy-MM-dd\")); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String \"01-01-1990\": not a valid representation (error: Failed to parse Date value '01-01-1990': Cannot parse date \"01-01-1990\": not compatible with any of standard forms (\"yyyy-MM-dd'T'HH:mm:ss.SSSZ\", \"yyyy-MM-dd'T'HH:mm:ss.SSS\", \"EEE, dd MMM yyyy HH:mm:ss zzz\", \"yyyy-MM-dd\"))\n at [Source: (PushbackInputStream); line: 1, column: 57] (through reference chain: java.util.ArrayList[0]->com.css.vob.model.UserDetails[\"dob\"])",
编辑-尝试@Pijotrek 的解决方案后,我现在收到此错误。
"message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
这是我的实体类、DAO 和 JPARepository
This is entity class of UserDetails
这是我从后控制器调用的用户详细信息的控制器方法-
@GetMapping("/userdetailsbyareacode/{areacode}")
public ResponseEntity<List<UserDetails>> getUserByAreacode(@PathVariable(value="areacode") Long areacode){
List<UserDetails> user=userdetailsService.getuserByAreacode(areacode);
if(user==null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().body(user);
}
这里是后置控制器的 JPARepository-
public interface PostRepository extends JpaRepository<Posts,Long> {
@Transactional
@Query(nativeQuery=true,value="SELECT * FROM POSTS WHERE U_ID=:u_id")
List<Posts> findPostsByU_id(@Param("u_id") Long u_id);
【问题讨论】:
标签: spring spring-boot resttemplate