【问题标题】:Error in postman while fetching data from another service using resttemplate使用resttemplate从另一个服务获取数据时邮递员出错
【发布时间】: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


    【解决方案1】:

    您在请求中发送包含一些用户详细信息的正文。

    ResponseEntity<List<UserDetails>> response= restTemplate.exchange("http://localhost:8091/voiceofbengal/userdetailsbyareacode/"+areacode, HttpMethod.GET,
    null, new ParameterizedTypeReference<List<UserDetails>>(){
    });
    

    阅读错误我假设您的UserDetails 类有一个名为dob 的字段。您在 Controller 类中访问的 REST API(顺便说一句,这是一种不好的做法)尝试转换该 dob 字段并获得异常,因为它不是以任何可接受的格式编写的。接受的格式是:(\"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\"));。这一切都在您的错误消息中。 因此,将您的 dob 格式化为 "1990-01-01T10:00:00.000Z""1990-01-01",它应该可以正常工作。

    或者,如果您控制在 Controller 中调用的 REST API,则可以使用 @DateTimeFormat 注释来匹配您当前发送的格式,即:@DateTimeFormat(pattern = "dd-MM-yyyy")

    编辑

    您的返回类型对于执行本机查询是错误的。无论如何,您不必使用一个,您可以将您的方法名称更改为以下:

    @Transactional
    List<Posts> findPostsByUserId(Long id);
    

    这应该可以解决问题

    【讨论】:

    • 我试过这个-@Column(name="dob") @DateTimeFormat(pattern = "dd-MM-yyyy") @Past private Date dob;但是现在我在邮递员中收到此错误:“状态”:500,“错误”:“内部服务器错误”,“消息”:“无法提取结果集;SQL [n/a];嵌套异常是 org.hibernate。 exception.SQLGrammarException: 无法提取 ResultSet",
    • @KaustabhNag 能否在调用 API 之前尝试将您的 dob 日期从 01-01-1990 格式化为 1990-01-01
    • 我已经在我的 UserDetails 模型类中尝试了您的解决方案。 "@DateTimeFormat(pattern="yyyy-MM-dd") 私人日期日期;"但是在邮递员中点击 postcontroller 的 URL,我现在收到此错误。 "message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"" 现在怎么办?
    • @KaustabhNag 所以转换现在通过了,还有另一个问题,但需要更多细节,我猜不出为什么 ResultSet 不能被提取。需要实体,数据库表,DAO/JpaRepository 可能......没有它它是在猜测。
    • 我已经编辑了我的问题,因为我无法在此处发布代码。请检查
    猜你喜欢
    • 2017-05-26
    • 2020-07-01
    • 2020-04-07
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2020-08-10
    相关资源
    最近更新 更多