Backend Development 8 min read

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

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

Note the difference between @Controller and @RestController .

2. @HttpExchange for remote or local API

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

Implementation example:

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

3. Implementing Spring MVC Controller interface

<code>@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;
    }
}
</code>

4. Implementing HttpRequestHandler

<code>@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是以 '/' 开头");
    }
}
</code>

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

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

5. Implementing Servlet interface

<code>@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>");
    }
}
</code>

Requires a SimpleServletHandlerAdapter bean:

<code>@Bean
SimpleServletHandlerAdapter simpleServletHandlerAdapter() {
    return new SimpleServletHandlerAdapter();
}
</code>

6. @WebEndpoint for custom Actuator endpoints

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

7. RouterFunction bean for functional routing

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

8. Traditional HttpServlet

<code>@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>");
    }
}
</code>

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.

backendJavaSpring Bootcode examplesSpring 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

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