当前位置: 首页 > article >正文

茴香豆的茴的写法-SpringBoot处理客户端请求的几种方式

方式1:@Controller或者@RestController

/**
 * 方式1:@Controller
 * */
@RestController
public class AtController {
    @GetMapping("/at")
    public String at() {
        return "@Controller";
    }
}

方式2:@Component + Controller接口

/**
 * 方式2:@Component + Controller接口
 * */
@Component("/interface")
public class InterfaceController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.getOutputStream().write("Controller implements interface".getBytes(StandardCharsets.UTF_8));
        return null;
    }
}

方式3:@Component + HttpRequestHandler

/**
 * 方式3:@Component + HttpRequestHandler
 * */
@Component("/httpRequestHandler")
public class HttpRequestHandlerController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getOutputStream().write("Controller implements HttpRequestHandler".getBytes(StandardCharsets.UTF_8));
    }
}

方式4:@Bean + RouterFunction

/**
  * 方式4:RouterFunction,WebFlux也有类似的方式
  * */
 @Bean
 public RouterFunction routerFunction(){
     return RouterFunctions.route().GET("/routerFunction",
             request-> ServerResponse.ok().contentType(MediaType.TEXT_HTML).body("Controller using RouterFunction")
     ).build();
 }

知道这些方式有啥用呢?有些时候,我们需要在starter中内置一些controller,但是又不想被其他的组件扫描到(比如swagger),这个时候用处就来了。


http://www.kler.cn/news/315052.html

相关文章:

  • 并发与并行的区别:深入理解Go语言中的核心概念
  • 大模型安全风险分析
  • 如何在WordPress中添加事件Schema(分步指南)
  • 小世界网络 | “小世界”网络和无标度网络
  • 开源模型应用落地-qwen模型小试-Qwen2.5-7B-Instruct-快速体验(十三)
  • 695. 岛屿的最大面积
  • C# 访问Access存取图片
  • 实时流处理框架(如Flink、Spark Streaming)
  • 系统架构设计师:软件可靠性
  • Flyway 常见问题与解决方案
  • c语言编写程序,找出出现次数最高的数字 数字范围1-1000 时间复杂度不超过O(n)
  • html,css基础知识点笔记(二)
  • VB中的垃圾回收(Garbage Collection)机制
  • 二叉搜索树(附源码C++)
  • 将sqlite3移植到开发板上
  • frp内网穿透部署
  • vue一级、二级路由设计
  • 论文阅读-Demystifying Misconceptions in Social Bots Research
  • Ubuntu20.04 搜索不到任何蓝牙设备
  • 【SpringCloud】优雅实现远程调用 - OpenFeign
  • 鸿蒙【项目打包】- .hap 和 .app;(测试如何安装发的hap包)(应用上架流程)
  • 二二复制模式小程序商城开发
  • Python中的IPython:交互式的Python shell
  • 算法题之宝石与石头
  • 微服务、云计算、分布式开发全套课程课件,来原于企培和多年大厂工作提炼
  • el-form动态标题和输入值,并且最后一个输入框不校验
  • Python 课程16-OpenCV
  • C++门迷宫
  • C++高精度计时方法总结(测试函数运行时间)
  • Axios基本语法和前后端交互