【发布时间】:2020-10-14 10:12:20
【问题描述】:
控制器
@RequestMapping(value=ApiName.BLOG_LIST_FEW, method=RequestMethod.POST,produces=ApiName.CHARSET)
public ResponseEntity<ApiResponse> blogListFew(@RequestBody String user){
try {
UserReqModel userReq=JsonUtill.convertJsonToJava(user,UserReqModel.class);
Map<String,Object> resp=userService.blogListFew(userReq.getPageNo());
if( !resp.containsKey("error")) {
return responseUtil.successResponse(resp);
}
else {
return responseUtil.failRsponse(resp.get("error"));
}
}
catch(Exception ex) {
return responseUtil.badResponse(ex);
}
}
服务
public Map<String, Object> blogListFew(Integer pageNo) {
Map<String, Object> resp = new HashMap<>();
List<Map<String, String>> arlMap = new ArrayList<>();
try {
int pageSize = 4;
System.out.println("here in the service 1");
System.out.println("here in the service 2");
Page<Blog> blogList = blogRepo.findByIsActiveOrderByCreateDateDesc(YesNO.YES,
PageRequest.of(pageNo, pageSize));
System.out.println("blogListb-->"+blogList);
if (blogList == null) {
resp.put("error", "not found");
return resp;
}
blogList.forEach(blog -> {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("description",
(blog.getDescription() != null) ? blog.getDescription().substring(0, 1) + "..." : "");
hashMap.put("blogTitle", (blog.getBlogTitle() != null) ? blog.getDescription() : "");
hashMap.put("verticalImageUrl", (blog.getImageVertUrl() != null) ? blog.getImageVertUrl() : "");
hashMap.put("blogId", blog.getBlogId().toString());
hashMap.put("isTrendingPriority", Integer.toString(blog.getTrendingPriority()));
arlMap.add(hashMap);
});
resp.put("totalPage", blogList.getTotalPages());
resp.put("blogList", arlMap);
return resp;
} catch (Exception ex) {
System.out.println(ex);
System.out.println(ex.getMessage());
resp.put("error","sxception occured");
return resp;
}
}
控制台输出-->
here in the service 1
here in the service 2
java.lang.NullPointerException
null
我正在检查没有博客表记录的情况,那次我调用了我的 api,但那时我知道它应该返回 null,因为我已经检查过,我会返回一些响应,但我不知道我是怎么做的我正在收到空指针异常,因为当它为空时甚至无法执行任何操作,甚至尝试打印 blogList 但即使该语句未执行,这意味着它在 jpa 查询方法中给出错误,并且当其中有记录时它工作正常.
我不知道它是如何获得空指针异常的,正是因为这个原因,我才在 bloglist 上进行了检查。 请任何人帮助我解决这个问题
【问题讨论】:
标签: spring-boot spring-data-jpa spring-data