9 Powerful Ways to Build Spring Boot 3 APIs – Full Code Collection

This guide showcases nine distinct Spring Boot 3 API development techniques—from classic @RestController annotations to functional RouterFunction beans and custom Actuator endpoints—providing complete code samples, configuration details, and a promise of ongoing updates with a downloadable PDF of over 90 practical cases.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
9 Powerful Ways to Build Spring Boot 3 APIs – Full Code Collection

Spring Boot 3 offers a variety of approaches for creating RESTful APIs, ranging from annotation‑based controllers to functional routing and custom Actuator endpoints.

1. Annotation‑based @RestController

@RestController
@RequestMapping("/pc")
public class PackController {
    @GetMapping("/index")
    public ResponseEntity<Object> index() {
        return ResponseEntity.ok("@Controller定义接口");
    }
}

Note the difference between @Controller and @RestController .

2. @HttpExchange for remote or local API

@HttpExchange("/users")
public interface RemoteUserAPI {
    @GetExchange("/{id}")
    ResponseEntity<String> getUser(@PathVariable Long id);
}

Implementation example:

@RestController
public class UserController implements RemoteUserAPI {
    @Override
    public ResponseEntity<String> getUser(Long id) {
        return ResponseEntity.ok("查询用户 - " + id);
    }
}

3. Implementing Spring MVC Controller interface

@Component("/pbnc")
public class PackControllerApi implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("我是通过实现Controller接口定义,并且beanName是以 '/' 开头");
        return null;
    }
}

4. Implementing HttpRequestHandler

@Component("/pbn")
public class PackHttpRequestHandlerApi implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request,
                              HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("我是通过实现HttpRequestHandler接口定义接口,并且beanName是以 '/' 开头");
    }
}

Register the handler with a ServletRegistrationBean and set the bean name accordingly:

@Bean
ServletRegistrationBean<HttpRequestHandlerServlet> httpRequestHandlerServlet() {
    HttpRequestHandlerServlet servlet = new HttpRequestHandlerServlet();
    ServletRegistrationBean<HttpRequestHandlerServlet> srb =
        new ServletRegistrationBean<>(servlet, "/xo");
    srb.setName("xxxooo");
    return srb;
}

5. Implementing Servlet interface

@Component("/psa")
public class PackServletApi extends GenericServlet {
    @Override
    public void service(ServletRequest req, ServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("<h3>我是通过继承GenericServlet实现的</h3>");
    }
}

Requires a SimpleServletHandlerAdapter bean:

@Bean
SimpleServletHandlerAdapter simpleServletHandlerAdapter() {
    return new SimpleServletHandlerAdapter();
}

6. @WebEndpoint for custom Actuator endpoints

@Component
@WebEndpoint(id = "packep")
public class PackEndpoint {
    @ReadOperation
    public ResponseEntity<Object> query(@Selector String name) {
        return ResponseEntity.ok("查询 - " + name);
    }
}

7. RouterFunction bean for functional routing

@Configuration
public class RouteConfig {
    @Bean
    RouterFunction<ServerResponse> r1() {
        return RouterFunctions.route()
                .GET("/r1", new ApiHandler())
                .build();
    }
    private static class ApiHandler implements HandlerFunction<ServerResponse> {
        @Override
        public ServerResponse handle(ServerRequest request) throws Exception {
            return ServerResponse.ok().body("我是通过定义RouterFunction实现的接口");
        }
    }
}

8. Traditional HttpServlet

@WebServlet("/pack")
public class PackServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("<h3>欢迎订阅学习《Spring Boot3实战案例锦集100例》</h3>");
    }
}

Enable scanning with @ServletComponentScan on the main application class.

The article promises permanent free updates for subscribers, providing a PDF ebook (currently 97 cases) and the complete source code for all examples.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaSpring BootSpring MVCAPI developmentActuator
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.