Java Web Application use Sprint Boot Client will appear the HTTP Status 500 error
Answered
I create a Sprint Boot project and Sprint Boot Client project (console mode). These connect with no problem. I want to create a Java Web Application with the Spring Boot pom.xml and reference the code of this web(as below) to create three buttons.
How do I call a specific Java method on click/submit event of specific button in JSP?
My button event code as below.
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
public class MyClass {
public MyClass()
{
}
public void method1()
{
System.out.println("method1");
RestTemplate restTemplate = new RestTemplate();
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
headers.setAccept(Collections.singletonList( org.springframework.http.MediaType.APPLICATION_JSON));
String resourceURL = "http://192.168.56.4:8080/run?name=/home/myuser/test";
org.springframework.http.HttpEntity<String> entity = new org.springframework.http.HttpEntity<String>(headers);
org.springframework.http.ResponseEntity<String> response2 = restTemplate.exchange(resourceURL, org.springframework.http.HttpMethod.GET,entity,String.class);
if (response2.getStatusCode() == org.springframework.http.HttpStatus.OK)
{
System.out.println("Hello World");
System.out.println(response2);
System.out.println(response2.getBody());
}
else
{
System.out.println("Error");
}
}
public void method2() {
//some code
System.out.println("method2");
}
public void method3() {
//some code
System.out.println("method3");
}
}
When I click one of the buttons, the web browser will appear the HTTP Status 500 - Internal Server Error as below.
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NoClassDefFoundError: org/springframework/util/MultiValueMap
com.mycompany.jsp.MyServlet.doPost(MyServlet.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.ClassNotFoundException: org.springframework.util.MultiValueMap
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1328)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
com.mycompany.jsp.MyServlet.doPost(MyServlet.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.13
If I comment the Spring Boot code in MyClass' method1, I click any button and there is no error appear.
Does somebody know how to integrate Spring Boot to Java Web Application?
Please sign in to leave a comment.
My pom.xml content is as below:
Hello,
Seems needed library is missing from the classpath. Is it possible to provide project example for investigation?