How to create an rest api in java?
7 answer(s)
Answer # 1 #
You can create a REST API in Java using Spring Boot. Create a new Spring Boot project, add spring-boot-starter-web dependency, and define a controller with @RestController and mapping annotations like @GetMapping or @PostMapping.
Answer # 2 #
For lightweight APIs, you can use JAX-RS with Jersey. Annotate your resource classes with @Path, @GET, and @Produces for REST endpoints.
Answer # 3 #
@RestController@RequestMapping("/api")public class MyController { @GetMapping("/hello") public String sayHello() { return "Hello World"; }}
Answer # 6 #
Ensure you handle exceptions properly and define appropriate HTTP status codes for different responses. Security can be added with Spring Security or JWT authentication.