Middle
How do you accept HTTP requests in a Java application? What options are there?
sobes.tech AI
Answer from AI
In Java, there are several ways to handle HTTP requests:
-
Servlet API — the traditional way through servlets, which are deployed in a servlet container (Tomcat, Jetty). It allows low-level request processing.
-
Spring Framework (Spring MVC / Spring Boot) — a popular framework that simplifies creating REST APIs and web applications. It allows annotating controller methods to handle HTTP requests.
-
Java EE / Jakarta EE — using JAX-RS to create RESTful web services.
-
Embedded HTTP servers — for example,
com.sun.net.httpserver.HttpServerfor simple cases without third-party libraries.
Example using Spring Boot:
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, world!";
}
}
Thus, the choice depends on project requirements and the technologies used.