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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC Rest 学习 一

發(fā)布時(shí)間:2023/12/20 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC Rest 学习 一 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

第一步:配置Spring MVC 核心Servlet

<!--?spring?mvc?--><listener><!--??request、session?和??global?session?web作用域?--><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><servlet><servlet-name>dispatcher</servlet-name><!--?Spring?mvc?核心分發(fā)器?--><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><!--?攔截所有請(qǐng)求,靜態(tài)資源會(huì)在spring-servlet中處理?--><url-pattern>/</url-pattern></servlet-mapping><filter><!--?utf-8?編碼處理?-->??<filter-name>Character?Encoding</filter-name>??<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>??<init-param>??<param-name>encoding</param-name>??<param-value>UTF-8</param-value>??</init-param>??</filter>??<filter-mapping>??<filter-name>Character?Encoding</filter-name>??<url-pattern>/</url-pattern>??</filter-mapping>

第二步:配置spring-servlet.xml

<?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context.xsd?http://www.springframework.org/schema/mvc?http://www.springframework.org/schema/mvc/spring-mvc.xsd?http://www.springframework.org/schema/util?http://www.springframework.org/schema/util/spring-util.xsd">??<!--?把標(biāo)記了@Controller注解的類轉(zhuǎn)換為bean?-->??<context:component-scan?base-package="com.hnust.controller"?/>??<!--?開啟MVC注解功能?,為了使Controller中的參數(shù)注解起效,需要如下配置?--><mvc:annotation-driven/><!--?靜態(tài)資源獲取,不用后臺(tái)映射?--><mvc:resources?mapping="/resource/**"?location="/resource/"/><mvc:interceptors>??<mvc:interceptor>??<mvc:mapping?path="/**"/>??<!--?定義在mvc:interceptor下面的表示是對(duì)特定的請(qǐng)求才進(jìn)行攔截的?-->??<bean?class="com.hnust.interceptor.TranslateInterceptor"/>??</mvc:interceptor>??</mvc:interceptors><!--?主要是進(jìn)行Controller?和?URL?的一些注解綁定,這里可以進(jìn)行轉(zhuǎn)換器配置:只有配置好了轉(zhuǎn)換器才能進(jìn)行類與JSON和XML的轉(zhuǎn)換,當(dāng)然只是針對(duì)基于轉(zhuǎn)換器協(xié)商資源表述?--><bean?class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"?/><!--?XML?與?Java?數(shù)據(jù)轉(zhuǎn)換??--><bean?id="jaxbMarshaller"?class="org.springframework.oxm.jaxb.Jaxb2Marshaller"><property?name="classesToBeBound"><list><!--common?XML?映射??JavaBean?注冊(cè)??--><value>com.hnust.bean.Resource</value></list></property></bean><!--?基于視圖渲染進(jìn)行協(xié)商資源表述??--><bean?class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"><!--?restful?是否采用擴(kuò)展名的方式確定內(nèi)容格式,id.json?返回JSON格式?--><property?name="favorPathExtension"?value="true"></property><!--?restful?是否采用參數(shù)支持確定內(nèi)容格式,id?format=json?返回JSON格式?--><property?name="favorParameter"?value="true"></property><!--?restful?是否忽略掉accept?header,Accept:application/json?--><property?name="ignoreAcceptHeader"?value="false"></property><!--?基于視圖按順序解析??--><property?name="order"?value="1"?/><!--?對(duì)采用擴(kuò)展名,參數(shù)新式的?URL?進(jìn)行獲取對(duì)應(yīng)的?accept??--><property?name="mediaTypes"><map><entry?key="json"?value="application/json"/><entry?key="xml"?value="application/xml"/><entry?key="html"?value="text/html"/></map></property><!--?如果擴(kuò)展名,參數(shù)甚至header?信息都沒有找到對(duì)應(yīng)的accept時(shí)??--><property?name="defaultContentType"?value="text/html"/><!--?采用對(duì)應(yīng)的視圖進(jìn)行渲染??--><property?name="defaultViews"><list?><!--?轉(zhuǎn)換Java對(duì)象為XML格式數(shù)據(jù)?--><bean?class="org.springframework.web.servlet.view.xml.MarshallingView"><constructor-arg?ref="jaxbMarshaller"?/></bean><!--?轉(zhuǎn)換Java對(duì)象為JSON?格式數(shù)據(jù)?--><bean?class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>?</list></property><!--?采用對(duì)應(yīng)的視圖進(jìn)行渲染??--><property?name="viewResolvers"><list?><!--?查找在上下文中定義了ID的Bean,并且定位該ID??--><bean?class="org.springframework.web.servlet.view.BeanNameViewResolver"/><!--?對(duì)Controller中返回的視圖實(shí)例進(jìn)行解析,并且組裝URL定位到對(duì)應(yīng)的資源??--><bean?id="viewResolver"?class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property?name="viewClass"?value="org.springframework.web.servlet.view.JstlView"/><property?name="prefix"?value="/WEB-INF/jsp/"/><property?name="suffix"?value=".jsp"/></bean></list></property></bean></beans>

第三步:編寫controller

/****@author:Heweipo*@version?1.00**/ @Controller @RequestMapping("/resource") public?class?ResourceController?{@Autowiredprivate?ResourceService?service;@RequestMapping(value="/get/{id}"?,?method=RequestMethod.GET)public?String?get(@PathVariable("id")?String?id?,?ModelMap?model){model.put("resource",?service.getResource(id));return?"resource";}}

第四步:頁面請(qǐng)求,獲取 Json xml html 三種格式的數(shù)據(jù)

瀏覽器請(qǐng)求URL:? 瀏覽器響應(yīng)結(jié)果:{"resource":{"id":"id_111","name":"maven","number":11}} ? 瀏覽器響應(yīng)結(jié)果: <resource><id>id_111</id><name>maven</name><number>11</number> </resource>

總結(jié):因?yàn)閯倓倢W(xué)習(xí),所以只是一個(gè)開始,下周貼出全部源碼,僅供學(xué)習(xí)參考。

轉(zhuǎn)載于:https://my.oschina.net/heweipo/blog/337581

總結(jié)

以上是生活随笔為你收集整理的Spring MVC Rest 学习 一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。