Sobes.tech
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:

  1. Servlet API — the traditional way through servlets, which are deployed in a servlet container (Tomcat, Jetty). It allows low-level request processing.

  2. 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.

  3. Java EE / Jakarta EE — using JAX-RS to create RESTful web services.

  4. Embedded HTTP servers — for example, com.sun.net.httpserver.HttpServer for 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.