Issue
I want to fetch data from database using spring boot with ajax but got error 500
. I check my all code but i can't find why got error from server. I can check below sql query in workbench
it's get perfect output but spring boot fire error.
GET http://localhost:8099/wholikepost?pstId=25 net::ERR_INVALID_CHUNKED_ENCODING 200
Here down is my code:
Repository & Service
// Repository
public interface PostLikeRepo extends JpaRepository<LikePost, Integer>{
@Query(nativeQuery = true, value = "SELECT * FROM like_master WHERE post_id = ?")
public List<LikePost> getTotalLikePopup(Integer id);
}
// Service
public class SomeServiceImpl implements SomeService{
@Autowired
private PostLikeRepo PostLikeRepo;
public List<LikePost> getTotalLikePopup(Integer Id){
return this.PostLikeRepo.getTotalLikePopup(Id);
}
}
Controller
@RequestMapping(value = "/wholikepost", method = RequestMethod.GET)
@ResponseBody
public List<LikePost> getTotalLikePopup(HttpServletRequest req)
{
List<LikePost> getWhoLikePost = this.SomeService.getTotalLikePopup(Integer.parseInt(req.getParameter("pstId")));
return getWhoLikePost;
}
Ajax Request
$(".like-style").on('click', function(){
let postId = parseInt($(this).attr("id").split("le")[1]); // get post id perfect
$.ajax({
type: "GET",
url: "/wholikepost",
data:{ pstId: postId },
success: function(res){
console.log(res);
}
});
});
Stacktrace
Caused by: java.io.IOException: An established connection was aborted by the software in your host machine
at java.base/sun.nio.ch.SocketDispatcher.write0(Native Method) ~[na:na]
at java.base/sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:54) ~[na:na]
at java.base/sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:113) ~[na:na]
at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:79) ~[na:na]
at java.base/sun.nio.ch.IOUtil.write(IOUtil.java:50) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:532) ~[na:na]
at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:135) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.doWrite(NioEndpoint.java:1364) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.util.net.SocketWrapperBase.doWrite(SocketWrapperBase.java:766) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.util.net.SocketWrapperBase.writeBlocking(SocketWrapperBase.java:586) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.tomcat.util.net.SocketWrapperBase.write(SocketWrapperBase.java:530) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.http11.Http11OutputBuffer$SocketOutputBuffer.doWrite(Http11OutputBuffer.java:547) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:112) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:194) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.coyote.Response.doWrite(Response.java:615) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:340) ~[tomcat-embed-core-9.0.50.jar:9.0.50]
... 67 common frames omitted
Solution
99% percent chances you are affected by the following issue
Your LikePost
entity has a circular dependency through a @ManyToMany
or @OneToMany
annotations, so when converting to JSON response it faces a stack overflow error which then get's hidden behind a closed connection with the client.
There is already a similar incident here
Check if you can add @JsonIgnore
in some of your @ManyToMany
or @OneToMany
and if this removes the error. Then you can be 100% sure that this was the cause.
Answered By - Panagiotis Bougioukos
Answer Checked By - David Marino (JavaFixing Volunteer)