Invoking other service URL using feign client gives error in postman

时间:2019-01-18 18:39:05

标签: spring-boot microservices

I have two services. One is UserDetails service and another is Post service. In userdetails I have a controller in which I am filtering the details of user using areacode.

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

now in post service I am calling this controller method using feign client and filter the post details based on areacode which is in the userdetails. Bothe userdetails and post service have u_id common. Based on u_id I am filtering the post details from my repository.

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);
}

And here is my post DAO-

public List<Posts> findByU_id(Long u_id){
    List<Posts> post=new ArrayList<>();
    postRepository.findPostsByU_id(u_id).forEach(post::add);
    return post;
}

Now this is the post controller method which I am calling in postman, which is using the feign proxy for calling the UserDetails controller method.

@GetMapping("/findpostbyareacode-feign/{areacode}")
public List<Posts> getPostByAreacodeUsingfeign(@PathVariable(value="areacode") Long areacode){

    List<UserDetails> usersList=userProxy.getUserByAreacode(areacode);

    List<Posts> postList=new ArrayList<>();
    List<Posts> totalPost=new ArrayList<>();
    for(UserDetails users : usersList) {
        totalPost=postService.findByU_id(users.getU_id());
        for(Posts posts : totalPost) {
            postList.add(posts);
        }
    }

    return postList;
}

When I am hitting the URL in postman, I am getting this error.

"error": "Internal Server Error", "message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet",

Here is my UserDetails entity class of UserDetails service.

@Entity
@Table(name="user_details")
@Builder
@Data
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
public class UserDetails implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="user_id", unique=true)
private Long user_id;

@Column(name="user_name", length=35, nullable=false)
@Size(min=2,message="Name should have atleast 2 character")
private String user_name;

@Column(name="gender", nullable=false)
private String gender;

@Column(name="dob")
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date dob;

@Column(name="email", length=50, unique=true, nullable=false)
private String email;

@Column(name="phno", length=11, nullable=false)
private String phno;

@Column(name="password", nullable=false)
//@JsonIgnore
private String password;

@Column(name="areacode", length=11)
private Long areacode;


@Column(name="u_Id")
private Long u_id;
}

And here is my post entity of Post service-

@Entity
@Table(name="vob_posts")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
public class Posts implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="post_id", unique=true)
private Long post_id;

@Column(name="contents")
private String contents;

@Column(name="u_id")
private Long u_id;
}

My both services are registered in eureka.

Cant solve this issue, pls help anyone.

1 个答案:

答案 0 :(得分:0)

why you are making code complex and How come primary key returns List<Posts>. Try with the below code.

Just make change below in your Entity and Repository.

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="user_id", unique=true)
   private Long id;
   //private Long userId;

   @Column(name="user_name", length=35, nullable=false)
   @Size(min=2,message="Name should have atleast 2 character")
   private String userName;

   public interface PostRepository extends JpaRepository<Posts,Long> 
   {

   }


   (or)

   public interface PostRepository extends CrudRepository<Posts,Long> 
   {
     //Optional<Posts> posts findByUserId(Long userId);
   }

  Service Layer : 
  Approach 1 : Using JPA Repository
  Posts posts = repo.getOne(userId);

  Approach 2 : Using Crud Repository
  Posts posts = repo.findById(userId).get();
  //Posts posts = repo.findByUserId(userId).get();