日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot(二):web综合开发

發布時間:2024/4/13 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot(二):web综合开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上篇文章介紹了Spring boot初級教程:spring boot(一):入門篇,方便大家快速入門、了解實踐Spring boot特性;本篇文章接著上篇內容繼續為大家介紹spring boot的其它特性(有些未必是spring boot體系桟的功能,但是是spring特別推薦的一些開源技術本文也會介紹),對了這里只是一個大概的介紹,特別詳細的使用我們會在其它的文章中來展開說明。

web開發

spring boot web開發非常的簡單,其中包括常用的json輸出、filters、property、log等

json 接口開發

在以前的spring 開發的時候需要我們提供json接口的時候需要做那些配置呢

  • 添加 jackjson 等相關jar包
  • 配置spring controller掃描
  • 對接的方法添加@ResponseBody
  • 就這樣我們會經常由于配置錯誤,導致406錯誤等等,spring boot如何做呢,只需要類添加?@RestController?即可,默認類中的方法都會以json的格式返回

    @RestController public class HelloWorldController {@RequestMapping("/getUser")public User getUser() {User user=new User();user.setUserName("小明");user.setPassWord("xxxx");return user;} }

    如果我們需要使用頁面開發只要使用@Controller?,下面會結合模板來說明

    自定義Filter

    我們常常在項目中會使用filters用于錄調用日志、排除有XSS威脅的字符、執行權限驗證等等。Spring Boot自動添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,并且我們可以自定義Filter。

    兩個步驟:

  • 實現Filter接口,實現Filter方法
  • 添加@Configurationz?注解,將自定義Filter加入過濾鏈
  • 好吧,直接上代碼

    @Configuration public class WebConfiguration {@Beanpublic RemoteIpFilter remoteIpFilter() {return new RemoteIpFilter();}@Beanpublic FilterRegistrationBean testFilterRegistration() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new MyFilter());registration.addUrlPatterns("/*");registration.addInitParameter("paramName", "paramValue");registration.setName("MyFilter");registration.setOrder(1);return registration;}public class MyFilter implements Filter {@Overridepublic void destroy() {// TODO Auto-generated method stub}@Overridepublic void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)throws IOException, ServletException {// TODO Auto-generated method stubHttpServletRequest request = (HttpServletRequest) srequest;System.out.println("this is MyFilter,url :"+request.getRequestURI());filterChain.doFilter(srequest, sresponse);}@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}} }

    自定義Property

    在web開發的過程中,我經常需要自定義一些配置文件,如何使用呢

    配置在application.properties中

    com.neo.title=純潔的微笑 com.neo.description=分享生活和技術

    自定義配置類

    @Component public class NeoProperties {@Value("${com.neo.title}")private String title;@Value("${com.neo.description}")private String description;//省略getter settet方法}

    log配置

    配置輸出的地址和輸出級別

    logging.path=/user/local/log logging.level.com.favorites=DEBUG logging.level.org.springframework.web=INFO logging.level.org.hibernate=ERROR

    path為本機的log地址,logging.level?后面可以根據包路徑配置不同資源的log級別

    數據庫操作

    在這里我重點講述mysql、spring data jpa的使用,其中mysql 就不用說了大家很熟悉,jpa是利用Hibernate生成各種自動化的sql,如果只是簡單的增刪改查,基本上不用手寫了,spring內部已經幫大家封裝實現了。

    下面簡單介紹一下如何在spring boot中使用

    1、添加相jar包

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>

    總結

    以上是生活随笔為你收集整理的spring boot(二):web综合开发的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。