{"id":1936,"date":"2023-04-01T10:00:07","date_gmt":"2023-04-01T02:00:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1936"},"modified":"2023-04-22T08:46:47","modified_gmt":"2023-04-22T00:46:47","slug":"controlleradvice-of-spring-boot","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/controlleradvice-of-spring-boot\/","title":{"rendered":"Spring Boot\u4e4b@ControllerAdvice"},"content":{"rendered":"<ul>\n<li><code>@ControllerAdvice<\/code>\u5c31\u662f<code>@Controller<\/code>\u589e\u5f3a\u7248<\/li>\n<li><code>@ControllerAdvice<\/code>\u4e3b\u8981\u7528\u6765\u5904\u7406\u5168\u5c40\u6570\u636e\uff0c\u4e00\u822c\u642d\u914d<code>@ExceptionHandler<\/code>\u3001<code>@ModelAttribute<\/code>\u3001<code>@InitBinder<\/code>\u4f7f\u7528<\/li>\n<\/ul>\n<h2>\u5168\u5c40\u5f02\u5e38\u5904\u7406<\/h2>\n<p>\u65b9\u6cd5\u7684\u53c2\u6570\u53ef\u4ee5\u6709\u5f02\u5e38\u5b9e\u4f8b\uff0c<code>HttpServletRequest<\/code>\u3001<code>HttpServletResponse<\/code>\u3001<code>Model<\/code>\u7b49<\/p>\n<p><!-- more --><\/p>\n<p>\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u662f\u4e00\u6bb5JSON\uff0c\u4e00\u4e2aModelAndView\uff0c\u4e00\u4e2a\u903b\u8f91\u89c6\u56fe\u540d\u7b49<\/p>\n<ol>\n<li>\u76f4\u63a5\u5904\u7406<\/li>\n<\/ol>\n<pre><code class=\"language-java\">@ControllerAdvice\npublic class CustomExceptionHandler {\n\n    @ExceptionHandler(MaxUploadSizeExceededException.class)\n    public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse response) throws IOException {\n        response.setContentType(&quot;text\/html;charset=utf-8&quot;);\n        PrintWriter out = response.getWriter();\n        out.write(&quot;\u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u8d85\u51fa\u9650\u5236!&quot;);\n        out.flush();\n        out.close();\n    }\n}<\/code><\/pre>\n<ol start=\"2\">\n<li>\u8fd4\u56deModelAndView<\/li>\n<\/ol>\n<pre><code class=\"language-java\">\/\/ 1.\u8bbe\u7f6e\u5168\u5c40\u5f02\u5e38\u5904\u7406\n@ControllerAdvice\npublic class CustomExceptionHandler2 {\n\n    @ExceptionHandler(MaxUploadSizeExceededException.class)\n    public ModelAndView uploadException(MaxUploadSizeExceededException e) throws IOException {\n        ModelAndView modelAndView = new ModelAndView();\n        modelAndView.addObject(&quot;msg&quot;, &quot;\u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u8d85\u51fa\u9650\u5236...&quot;);\n        modelAndView.setViewName(&quot;error&quot;);\n        return modelAndView;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-html\">&lt;!-- 2.\u5bfc\u5165thymeleaf\u4f9d\u8d56 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;!-- 3.resource\u76ee\u5f55\u4e0b\u7684templates\u76ee\u5f55\u4e0b\u6dfb\u52a0error.html --&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot; xmlns:th=&quot;http:\/\/www.thymeleaf.org&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;Title&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div th:text=&quot;${msg}&quot;&gt;&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u6dfb\u52a0\u5168\u5c40\u6570\u636e<\/h2>\n<ol>\n<li>\u6dfb\u52a0\u5168\u5c40\u6570\u636e<\/li>\n<\/ol>\n<pre><code class=\"language-java\">@ControllerAdvice\npublic class GlobalConfig {\n\n    @ModelAttribute(value = &quot;info&quot;)\n    public Map&lt;String,String&gt; userInfo() {\n        HashMap&lt;String, String&gt; map = new HashMap&lt;&gt;();\n        map.put(&quot;author&quot;, &quot;\u7f57\u8d2f\u4e2d&quot;);\n        map.put(&quot;gender&quot;, &quot;\u7537&quot;);\n        return map;\n    }\n}<\/code><\/pre>\n<ol start=\"2\">\n<li>\u53d6\u51fa\u5168\u5c40\u6570\u636e<\/li>\n<\/ol>\n<pre><code class=\"language-java\">@GetMapping(&quot;\/hello&quot;)\npublic void hello(Model model) {\n    Iterator&lt;Map.Entry&lt;String, Object&gt;&gt; iterator = model.asMap().entrySet().iterator();\n    while (iterator.hasNext()){\n        Map.Entry&lt;String, Object&gt; map = iterator.next();\n        String key = map.getKey();\n        Object value = map.getValue();\n        System.out.println(key + &quot;: &quot; + value);\n        \/\/info: {gender=\u7537, author=\u7f57\u8d2f\u4e2d}\n    }\n}<\/code><\/pre>\n<h2>\u8bf7\u6c42\u53c2\u6570\u9884\u5904\u7406<\/h2>\n<p><code>@ControllerAdvice<\/code>\u7ed3\u5408<code>@InitBinder<\/code>\u8fd8\u80fd\u5b9e\u73b0\u8bf7\u6c42\u53c2\u6570\u9884\u5904\u7406\uff0c\u5373\u5c06\u8868\u5355\u4e2d\u7684\u6570\u636e\u7ed1\u5b9a\u5230\u5b9e\u4f53\u7c7b\u4e0a\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u5904\u7406\u3002<\/p>\n<ol>\n<li>\u53c2\u6570\u4f20\u9012\u65f6\uff0c\u4e24\u4e2a\u5b9e\u4f53\u7c7b\u7684name\u5c5e\u6027\u6df7\u6dc6<\/li>\n<\/ol>\n<pre><code>public class Author {\n    private String name;\n    private int age;\n    \/\/setter getter...\n}\n\npublic class Book {\n    private String name;\n    private String author;\n    \/\/setter getter...\n}\n\n@GetMapping(&quot;\/book&quot;)\n@ResponseBody\npublic String book(Book book, Author author) {\n    return book.toString() + &quot;&gt;&gt;&gt;&quot; + author.toString();\n}\n\n\/\/\u8bf7\u6c42\uff1ahttp:\/\/localhost:8080\/book?name=\u5f20\u4e09&amp;age=18&amp;name=\u4e09\u56fd\u6f14\u4e49&amp;author=\u7f57\u8d2f\u4e2d\n\/\/\u8fd4\u56de\u6570\u636e\uff1aBook{name=&#039;\u5f20\u4e09,\u4e09\u56fd\u6f14\u4e49&#039;, author=&#039;\u7f57\u8d2f\u4e2d&#039;}&gt;&gt;&gt;Author{name=&#039;\u5f20\u4e09,\u4e09\u56fd\u6f14\u4e49&#039;, age=18}<\/code><\/pre>\n<ol start=\"2\">\n<li>\u53c2\u6570\u9884\u5904\u7406\uff0c\u89e3\u51b3\u4e24\u4e2aBean\u540c\u540d\u5c5e\u6027\u6df7\u6dc6<\/li>\n<\/ol>\n<pre><code class=\"language-java\">\/\/ 1.\u5c06Controller\u65b9\u6cd5\u53c2\u6570\u901a\u8fc7@ModelAttribute(&quot;a&quot;)\u7ed1\u5b9a\u5c5e\u6027a\u548cb\n@GetMapping(&quot;\/book&quot;)\n@ResponseBody\npublic String book(@ModelAttribute(&quot;b&quot;) Book book, @ModelAttribute(&quot;a&quot;) Author author){\n    return book.toString() + &quot;&gt;&gt;&gt;&quot; + author.toString();\n}\n\n\/\/ 2.1\u5728ControllerAdvice\u4e2d\u7528@InitBinder(&quot;a&quot;)\u6765\u8868\u793a\u5904\u7406@ModelAttribute(&quot;a&quot;)\u4e2d\u7684\u53c2\u6570\n\/\/ 2.2\u901a\u8fc7WebDateBinder\u6765\u8bbe\u7f6e\u5b57\u6bb5\u524d\u7f00\n@ControllerAdvice\npublic class GlobalConfig2 {\n\n    @InitBinder(&quot;b&quot;)\n    public void init(WebDataBinder binder) {\n        binder.setFieldDefaultPrefix(&quot;b.&quot;);\n    }\n\n    @InitBinder(&quot;a&quot;)\n    public void init2(WebDataBinder binder) {\n        binder.setFieldDefaultPrefix(&quot;a.&quot;);\n    }\n}\n\n\/\/\u8bf7\u6c42\uff1ahttp:\/\/localhost:8080\/book?a.name=\u5f20\u4e09&amp;a.age=18&amp;b.name=\u4e09\u56fd\u6f14\u4e49&amp;b.author=\u7f57\u8d2f\u4e2d\n\/\/\u8fd4\u56de\u6570\u636e\uff1aBook{name=&#039;\u4e09\u56fd\u6f14\u4e49&#039;, author=&#039;\u7f57\u8d2f\u4e2d&#039;}&gt;&gt;&gt;Author{name=&#039;\u5f20\u4e09&#039;, age=18}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>@ControllerAdvice\u5c31\u662f@Controller\u589e\u5f3a\u7248 @ControllerAdvice\u4e3b\u8981\u7528\u6765 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-1936","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1936"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1936\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}