Spring--spring3.0应用springmvc构造RESTful URL详细讲解

本文主要是介绍Spring--spring3.0应用springmvc构造RESTful URL详细讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载自:http://blog.csdn.net/yczz/article/details/5905893


springmvc 3.0 中增加 RESTful URL功能,构造出类似javaeye现在的URL。 rest介绍 , 这里还有struts2 rest构造的一篇文章: 使用 Struts 2 开发 RESTful 服务

简单例子如下,比如如下URL

Java代码  复制代码
  1. /blog/1  HTTP GET =>    得到id = 1的blog   
  2. /blog/1  HTTP DELETE => 删除 id = 1的blog   
  3. /blog/1  HTTP PUT  =>   更新id = 1的blog   
  4. /blog     HTTP POST =>   新增BLOG  
[java]  view plain copy
  1. /blog/1  HTTP GET =>    得到id = 1的blog  
  2. /blog/1  HTTP DELETE => 删除 id = 1的blog  
  3. /blog/1  HTTP PUT  =>   更新id = 1的blog  
  4. /blog     HTTP POST =>   新增BLOG  

 

 

以下详细解一下spring rest使用.

 

首先,我们带着如下三个问题查看本文。
1. 如何在Java构造没有扩展名的RESTful url,如 /forms/1,而不是 /forms/1.do

2. 由于我们要构造没有扩展名的url本来是处理静态资源的容器映射的,现在被我们的spring占用了,冲突怎么解决?
3. 浏览器的form标签不支持提交delete,put请求,如何曲线解决?

 

springmvc rest 实现


springmvc的resturl是通过@RequestMapping 及@PathVariable annotation提供的,通过如@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)即可处理/blog/1 的delete请求.

Java代码  复制代码
  1. @RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)   
  2. public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {   
  3.     blogManager.removeById(id);   
  4.     return new ModelAndView(LIST_ACTION);   
  5. }  
[java]  view plain copy
  1. @RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)  
  2. public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {  
  3.     blogManager.removeById(id);  
  4.     return new ModelAndView(LIST_ACTION);  
  5. }  

 

@RequestMapping @PathVariable如果URL中带参数,则配合使用,如

Java代码  复制代码
  1. @RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)   
  2. public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {   
  3. }  
[java]  view plain copy
  1. @RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)  
  2. public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId") Long msgId,HttpServletRequest request,HttpServletResponse response) {  
  3. }  

 

 spring rest配置指南

1. springmvc web.xml配置

Xml代码  复制代码
  1. <!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->  
  2. <servlet-mapping>  
  3.     <servlet-name>default</servlet-name>  
  4.     <url-pattern>/static/*</url-pattern>  
  5. </servlet-mapping>  
  6. <servlet>  
  7.     <servlet-name>springmvc</servlet-name>  
  8.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  9.     <load-on-startup>1</load-on-startup>  
  10. </servlet>  
  11.   
  12. <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->  
  13. <filter>  
  14.     <filter-name>UrlRewriteFilter</filter-name>  
  15.     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
  16.     <init-param>  
  17.             <param-name>confReloadCheckInterval</param-name>  
  18.             <param-value>60</param-value>  
  19.         </init-param>  
  20.     <init-param>  
  21.                 <param-name>logLevel</param-name>  
  22.                 <param-value>DEBUG</param-value>  
  23.         </init-param>        
  24. </filter>  
  25. <filter-mapping>  
  26.     <filter-name>UrlRewriteFilter</filter-name>  
  27.     <url-pattern>/*</url-pattern>  
  28. </filter-mapping>  
  29.   
  30. <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->  
  31. <servlet-mapping>  
  32.     <servlet-name>springmvc</servlet-name>  
  33.     <url-pattern>/</url-pattern>  
  34. </servlet-mapping>  
  35.   
  36. <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->  
  37. <filter>  
  38.     <filter-name>HiddenHttpMethodFilter</filter-name>  
  39.     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  40. </filter>  
  41.   
  42. <filter-mapping>  
  43.     <filter-name>HiddenHttpMethodFilter</filter-name>  
  44.     <servlet-name>springmvc</servlet-name>  
  45. </filter-mapping>  
[xml]  view plain copy
  1. <!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->  
  2. <servlet-mapping>  
  3.     <servlet-name>default</servlet-name>  
  4.     <url-pattern>/static/*</url-pattern>  
  5. </servlet-mapping>  
  6. <servlet>  
  7.     <servlet-name>springmvc</servlet-name>  
  8.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  9.     <load-on-startup>1</load-on-startup>  
  10. </servlet>  
  11.   
  12. <!-- URL重写filter,用于将访问静态资源http://localhost/foo.css 转为http://localhost/static/foo.css -->  
  13. <filter>  
  14.     <filter-name>UrlRewriteFilter</filter-name>  
  15.     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
  16.     <init-param>  
  17.             <param-name>confReloadCheckInterval</param-name>  
  18.             <param-value>60</param-value>  
  19.         </init-param>  
  20.     <init-param>  
  21.                 <param-name>logLevel</param-name>  
  22.                 <param-value>DEBUG</param-value>  
  23.         </init-param>       
  24. </filter>  
  25. <filter-mapping>  
  26.     <filter-name>UrlRewriteFilter</filter-name>  
  27.     <url-pattern>/*</url-pattern>  
  28. </filter-mapping>  
  29.   
  30. <!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->  
  31. <servlet-mapping>  
  32.     <servlet-name>springmvc</servlet-name>  
  33.     <url-pattern>/</url-pattern>  
  34. </servlet-mapping>  
  35.   
  36. <!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->  
  37. <filter>  
  38.     <filter-name>HiddenHttpMethodFilter</filter-name>  
  39.     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  40. </filter>  
  41.   
  42. <filter-mapping>  
  43.     <filter-name>HiddenHttpMethodFilter</filter-name>  
  44.     <servlet-name>springmvc</servlet-name>  
  45. </filter-mapping>  

 

 

2. webapp/WEB-INF/springmvc-servlet.xml配置,使用如下两个class激活@RequestMapping annotation

Java代码  复制代码
  1. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>   
  2. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
[java]  view plain copy
  1. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
  2. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  

 

完整配置

Java代码  复制代码
  1. <beans default-autowire="byName"   >   
  2.   
  3.     <!-- 自动搜索@Controller标注的类 -->   
  4.     <context:component-scan base-package="com.**.controller"/>   
  5.        
  6.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>   
  7.   
  8.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>   
  9.   
  10.     <!-- Default ViewResolver -->   
  11.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">   
  12.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>   
  13.         <property name="prefix" value="/pages"/>   
  14.         <property name="suffix" value=".jsp"></property>   
  15.     </bean>   
  16.        
  17.     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>   
  18.   
  19.     <!-- Mapping exception to the handler view -->   
  20.     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">   
  21.         <!-- to /commons/error.jsp -->   
  22.         <property name="defaultErrorView" value="/commons/error"/>   
  23.         <property name="exceptionMappings">   
  24.             <props>   
  25.             </props>   
  26.         </property>   
  27.     </bean>   
  28.            
  29. </beans>  
[java]  view plain copy
  1. <beans default-autowire="byName"   >  
  2.   
  3.     <!-- 自动搜索@Controller标注的类 -->  
  4.     <context:component-scan base-package="com.**.controller"/>  
  5.       
  6.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
  7.   
  8.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  9.   
  10.     <!-- Default ViewResolver -->  
  11.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  12.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
  13.         <property name="prefix" value="/pages"/>  
  14.         <property name="suffix" value=".jsp"></property>  
  15.     </bean>  
  16.       
  17.     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages"/>  
  18.   
  19.     <!-- Mapping exception to the handler view -->  
  20.     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
  21.         <!-- to /commons/error.jsp -->  
  22.         <property name="defaultErrorView" value="/commons/error"/>  
  23.         <property name="exceptionMappings">  
  24.             <props>  
  25.             </props>  
  26.         </property>  
  27.     </bean>  
  28.           
  29. </beans>  

 

 

3. Controller编写

Java代码  复制代码
  1. /**  
  2.  * @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一,  
  3.  * 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new  
  4.  */  
  5. @Controller  
  6. @RequestMapping("/userinfo")   
  7. public class UserInfoController extends BaseSpringController{   
  8.     //默认多列排序,example: username desc,createTime asc   
  9.     protected static final String DEFAULT_SORT_COLUMNS = null;    
  10.        
  11.     private UserInfoManager userInfoManager;   
  12.        
  13.     private final String LIST_ACTION = "redirect:/userinfo";   
  14.        
  15.     /**   
  16.      * 通过spring自动注入  
  17.      **/  
  18.     public void setUserInfoManager(UserInfoManager manager) {   
  19.         this.userInfoManager = manager;   
  20.     }   
  21.        
  22.     /** 列表 */  
  23.     @RequestMapping  
  24.     public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {   
  25.         PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);   
  26.         //pageRequest.getFilters(); //add custom filters   
  27.            
  28.         Page page = this.userInfoManager.findByPageRequest(pageRequest);   
  29.         savePage(page,pageRequest,request);   
  30.         return new ModelAndView("/userinfo/list","userInfo",userInfo);   
  31.     }   
  32.        
  33.     /** 进入新增 */  
  34.     @RequestMapping(value="/new")   
  35.     public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {   
  36.         return new ModelAndView("/userinfo/new","userInfo",userInfo);   
  37.     }   
  38.        
  39.     /** 显示 */  
  40.     @RequestMapping(value="/{id}")   
  41.     public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {   
  42.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);   
  43.         return new ModelAndView("/userinfo/show","userInfo",userInfo);   
  44.     }   
  45.        
  46.     /** 编辑 */  
  47.     @RequestMapping(value="/{id}/edit")   
  48.     public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {   
  49.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);   
  50.         return new ModelAndView("/userinfo/edit","userInfo",userInfo);   
  51.     }   
  52.        
  53.     /** 保存新增 */  
  54.     @RequestMapping(method=RequestMethod.POST)   
  55.     public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {   
  56.         userInfoManager.save(userInfo);   
  57.         return new ModelAndView(LIST_ACTION);   
  58.     }   
  59.        
  60.     /** 保存更新 */  
  61.     @RequestMapping(value="/{id}",method=RequestMethod.PUT)   
  62.     public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {   
  63.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);   
  64.         bind(request,userInfo);   
  65.         userInfoManager.update(userInfo);   
  66.         return new ModelAndView(LIST_ACTION);   
  67.     }   
  68.        
  69.     /** 删除 */  
  70.     @RequestMapping(value="/{id}",method=RequestMethod.DELETE)   
  71.     public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {   
  72.         userInfoManager.removeById(id);   
  73.         return new ModelAndView(LIST_ACTION);   
  74.     }   
  75.   
  76.     /** 批量删除 */  
  77.     @RequestMapping(method=RequestMethod.DELETE)   
  78.     public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {   
  79.            
  80.         for(int i = 0; i < items.length; i++) {   
  81.                
  82.             userInfoManager.removeById(items[i]);   
  83.         }   
  84.         return new ModelAndView(LIST_ACTION);   
  85.     }   
  86.        
  87. }  
[java]  view plain copy
  1. /** 
  2.  * @RequestMapping("/userinfo") 具有层次关系,方法级的将在类一级@RequestMapping之一, 
  3.  * 如下面示例, 访问方法级别的@RequestMapping("/new"),则URL为 /userinfo/new 
  4.  */  
  5. @Controller  
  6. @RequestMapping("/userinfo")  
  7. public class UserInfoController extends BaseSpringController{  
  8.     //默认多列排序,example: username desc,createTime asc  
  9.     protected static final String DEFAULT_SORT_COLUMNS = null;   
  10.       
  11.     private UserInfoManager userInfoManager;  
  12.       
  13.     private final String LIST_ACTION = "redirect:/userinfo";  
  14.       
  15.     /**  
  16.      * 通过spring自动注入 
  17.      **/  
  18.     public void setUserInfoManager(UserInfoManager manager) {  
  19.         this.userInfoManager = manager;  
  20.     }  
  21.       
  22.     /** 列表 */  
  23.     @RequestMapping  
  24.     public ModelAndView index(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) {  
  25.         PageRequest<Map> pageRequest = newPageRequest(request,DEFAULT_SORT_COLUMNS);  
  26.         //pageRequest.getFilters(); //add custom filters  
  27.           
  28.         Page page = this.userInfoManager.findByPageRequest(pageRequest);  
  29.         savePage(page,pageRequest,request);  
  30.         return new ModelAndView("/userinfo/list","userInfo",userInfo);  
  31.     }  
  32.       
  33.     /** 进入新增 */  
  34.     @RequestMapping(value="/new")  
  35.     public ModelAndView _new(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {  
  36.         return new ModelAndView("/userinfo/new","userInfo",userInfo);  
  37.     }  
  38.       
  39.     /** 显示 */  
  40.     @RequestMapping(value="/{id}")  
  41.     public ModelAndView show(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
  42.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);  
  43.         return new ModelAndView("/userinfo/show","userInfo",userInfo);  
  44.     }  
  45.       
  46.     /** 编辑 */  
  47.     @RequestMapping(value="/{id}/edit")  
  48.     public ModelAndView edit(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
  49.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);  
  50.         return new ModelAndView("/userinfo/edit","userInfo",userInfo);  
  51.     }  
  52.       
  53.     /** 保存新增 */  
  54.     @RequestMapping(method=RequestMethod.POST)  
  55.     public ModelAndView create(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo) throws Exception {  
  56.         userInfoManager.save(userInfo);  
  57.         return new ModelAndView(LIST_ACTION);  
  58.     }  
  59.       
  60.     /** 保存更新 */  
  61.     @RequestMapping(value="/{id}",method=RequestMethod.PUT)  
  62.     public ModelAndView update(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) throws Exception {  
  63.         UserInfo userInfo = (UserInfo)userInfoManager.getById(id);  
  64.         bind(request,userInfo);  
  65.         userInfoManager.update(userInfo);  
  66.         return new ModelAndView(LIST_ACTION);  
  67.     }  
  68.       
  69.     /** 删除 */  
  70.     @RequestMapping(value="/{id}",method=RequestMethod.DELETE)  
  71.     public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response) {  
  72.         userInfoManager.removeById(id);  
  73.         return new ModelAndView(LIST_ACTION);  
  74.     }  
  75.   
  76.     /** 批量删除 */  
  77.     @RequestMapping(method=RequestMethod.DELETE)  
  78.     public ModelAndView batchDelete(@RequestParam("items") Long[] items,HttpServletRequest request,HttpServletResponse response) {  
  79.           
  80.         for(int i = 0; i < items.length; i++) {  
  81.               
  82.             userInfoManager.removeById(items[i]);  
  83.         }  
  84.         return new ModelAndView(LIST_ACTION);  
  85.     }  
  86.       
  87. }  

 

 

上面是rapid-framework 新版本生成器生成的代码,以后也将应用此规则,rest url中增删改查等基本方法与Controller的方法映射规则

Java代码  复制代码
  1. /userinfo           => index()   
  2. /userinfo/new       => _new()   
  3. /userinfo/{id}      => show()   
  4. /userinfo/{id}/edit         => edit()   
  5. /userinfo   POST        => create()   
  6. /userinfo/{id}  PUT => update()   
  7. /userinfo/{id}  DELETE  => delete()   
  8. /userinfo   DELETE      => batchDelete()  
[java]  view plain copy
  1. /userinfo           => index()  
  2. /userinfo/new       => _new()  
  3. /userinfo/{id}      => show()  
  4. /userinfo/{id}/edit         => edit()  
  5. /userinfo   POST        => create()  
  6. /userinfo/{id}  PUT => update()  
  7. /userinfo/{id}  DELETE  => delete()  
  8. /userinfo   DELETE      => batchDelete()  

 注(不使用 /userinfo/add  => add() 方法是由于add这个方法会被maxthon浏览器当做广告链接过滤掉,因为包含ad字符)

 

4. jsp 编写

Html代码  复制代码
  1. <form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">  
  2. </form:form>  
[html]  view plain copy
  1. <form:form action="${ctx}/userinfo/${userInfo.userId}" method="put">  
  2. </form:form>  

 生成的html内容如下, 生成一个hidden的_method=put,并于web.xml中的HiddenHttpMethodFilter配合使用,在服务端将post请求改为put请求

Java代码  复制代码
  1. <form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">   
  2.     <input type="hidden" name="_method" value="put"/>   
  3. </form>  
[java]  view plain copy
  1. <form id="userInfo" action="/springmvc_rest_demo/userinfo/2" method="post">  
  2.     <input type="hidden" name="_method" value="put"/>  
  3. </form>  

 

另外一种方法是你可以使用ajax发送put,delete请求.

 

5. 静态资源的URL重写

   如上我们描述,现因为将default servlet映射至/static/的子目录,现我们访问静态资源将会带一个/static/前缀.

   如 /foo.gif, 现在访问该文件将是 /static/foo.gif.
   那如何避免这个前缀呢,那就是应用URL rewrite,现我们使用 http://tuckey.org/urlrewrite/, 重写规则如下

 

Xml代码  复制代码
  1. <urlrewrite>  
  2.     <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->  
  3.     <rule>  
  4.         <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>  
  5.         <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>  
  6.         <from>^(/.*/..*)$</from>  
  7.         <to>/static$1</to>  
  8.     </rule>  
  9. </urlrewrite>  
[xml]  view plain copy
  1. <urlrewrite>  
  2.     <!-- 访问jsp及jspx将不rewrite url,其它.js,.css,.gif等将重写,如 /foo.gif => /static/foo.gif -->  
  3.     <rule>  
  4.         <condition operator="notequal" next="and" type="request-uri">.*.jsp</condition>  
  5.         <condition operator="notequal" next="and" type="request-uri">.*.jspx</condition>  
  6.         <from>^(/.*/..*)$</from>  
  7.         <to>/static$1</to>  
  8.     </rule>  
  9. </urlrewrite>  

   另笔者专门写了一个 RestUrlRewriteFilter来做同样的事件,以后会随着rapid-framework一起发布. 比这个更加轻量级.

 

并且该代码已经贡献给spring,不知会不会在下一版本发布


这篇关于Spring--spring3.0应用springmvc构造RESTful URL详细讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/788619

相关文章

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B