spring-boot - 使用 feign 客户端调用其他服务 URL 会导致邮递员出错
问题描述
我有两个服务。一个是 UserDetails 服务,另一个是 Post 服务。在 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);
}
现在在后期服务中,我使用 feign 客户端调用此控制器方法,并根据用户详细信息中的区域代码过滤帖子详细信息。userdetails 和 post service 都有 u_id 通用。基于 u_id 我正在从我的存储库中过滤帖子详细信息。
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);
}
这是我的帖子 DAO-
public List<Posts> findByU_id(Long u_id){
List<Posts> post=new ArrayList<>();
postRepository.findPostsByU_id(u_id).forEach(post::add);
return post;
}
现在这是我在 postman 中调用的 post 控制器方法,它使用 feign 代理调用 UserDetails 控制器方法。
@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;
}
当我在邮递员中点击 URL 时,出现此错误。
“错误”:“内部服务器错误”,“消息”:“无法提取结果集;SQL [n/a];嵌套异常是 org.hibernate.exception.SQLGrammarException:无法提取结果集”,
这是我的 UserDetails 服务的 UserDetails 实体类。
@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;
}
这是我的邮政服务的邮政实体-
@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;
}
我的两个服务都在尤里卡注册。
无法解决这个问题,请帮助任何人。
解决方案
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();
推荐阅读
- python - Python BFS 没有给出最短路径
- c# - 如何将选定的列从 CSV 获取到我的 XML c#
- android - 滚动到最后一个位置 RecyclerView 时出现问题
- php - 循环复制自身的迭代
- google-cloud-platform - 在谷歌云计算上获取有关 VM 实例的完整信息
- javascript - React 同一组件的多个实例接收不同的数据
- apache-flink - Apache Flink 1.4.2 com.esotericsoftware.kryo.KryoException: java.lang.UnsupportedOperationException
- java - 为什么子类成员不受保护?
- c++ - tensorflow c++ API 是否支持反向传播的自动微分?
- html - Bootstrap 4.00 列没有换行