当前位置: 首页 > 图灵资讯 > java面试题> 金三银四精选面试题-SpringBoot的四种Handler类型

金三银四精选面试题-SpringBoot的四种Handler类型

来源:图灵教育
时间:2023-11-14 10:35:58
SpringBoot的四种Handler类型

1、@Controller+@RequestMapping
@RestController
public class ZhouyuController {
@GetMapping("/test")
public string test() {
return "zhouyu";
}

}

2、Controller接口
/**
* 作者:周瑜大都督
*/
@Component("/beanNameController")
public class ZhouyuBeanNameController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.getWriter().println("ZhouyuBeanNameController");
return null;
}
}
3、HttpRequestHandler
/**
* 作者:周瑜大都督
*/
@Component("/beanNameHandler")
public class ZhouyuBeanNameHandler implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().println("ZhouyuBeanNameHandler");
}
}
4、RouterFunction
@SpringBootApplication
public class MyApplication {

@Bean
public RouterFunction<ServerResponse> routerFunction(){
return route()
.GET("/getUserName", request -> ServerResponse.ok().body("zhouyu"))
.GET("/getUserAge", request -> ServerResponse.ok().body("88"))
.build();
}

public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}

}