當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring MVC Rest 学习 一
生活随笔
收集整理的這篇文章主要介紹了
Spring MVC Rest 学习 一
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
第一步:配置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?核心分發器?--><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><!--?攔截所有請求,靜態資源會在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">??<!--?把標記了@Controller注解的類轉換為bean?-->??<context:component-scan?base-package="com.hnust.controller"?/>??<!--?開啟MVC注解功能?,為了使Controller中的參數注解起效,需要如下配置?--><mvc:annotation-driven/><!--?靜態資源獲取,不用后臺映射?--><mvc:resources?mapping="/resource/**"?location="/resource/"/><mvc:interceptors>??<mvc:interceptor>??<mvc:mapping?path="/**"/>??<!--?定義在mvc:interceptor下面的表示是對特定的請求才進行攔截的?-->??<bean?class="com.hnust.interceptor.TranslateInterceptor"/>??</mvc:interceptor>??</mvc:interceptors><!--?主要是進行Controller?和?URL?的一些注解綁定,這里可以進行轉換器配置:只有配置好了轉換器才能進行類與JSON和XML的轉換,當然只是針對基于轉換器協商資源表述?--><bean?class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"?/><!--?XML?與?Java?數據轉換??--><bean?id="jaxbMarshaller"?class="org.springframework.oxm.jaxb.Jaxb2Marshaller"><property?name="classesToBeBound"><list><!--common?XML?映射??JavaBean?注冊??--><value>com.hnust.bean.Resource</value></list></property></bean><!--?基于視圖渲染進行協商資源表述??--><bean?class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"><!--?restful?是否采用擴展名的方式確定內容格式,id.json?返回JSON格式?--><property?name="favorPathExtension"?value="true"></property><!--?restful?是否采用參數支持確定內容格式,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"?/><!--?對采用擴展名,參數新式的?URL?進行獲取對應的?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><!--?如果擴展名,參數甚至header?信息都沒有找到對應的accept時??--><property?name="defaultContentType"?value="text/html"/><!--?采用對應的視圖進行渲染??--><property?name="defaultViews"><list?><!--?轉換Java對象為XML格式數據?--><bean?class="org.springframework.web.servlet.view.xml.MarshallingView"><constructor-arg?ref="jaxbMarshaller"?/></bean><!--?轉換Java對象為JSON?格式數據?--><bean?class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>?</list></property><!--?采用對應的視圖進行渲染??--><property?name="viewResolvers"><list?><!--?查找在上下文中定義了ID的Bean,并且定位該ID??--><bean?class="org.springframework.web.servlet.view.BeanNameViewResolver"/><!--?對Controller中返回的視圖實例進行解析,并且組裝URL定位到對應的資源??--><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";}}第四步:頁面請求,獲取 Json xml html 三種格式的數據
瀏覽器請求URL:? 瀏覽器響應結果:{"resource":{"id":"id_111","name":"maven","number":11}} ? 瀏覽器響應結果: <resource><id>id_111</id><name>maven</name><number>11</number> </resource>總結:因為剛剛學習,所以只是一個開始,下周貼出全部源碼,僅供學習參考。
轉載于:https://my.oschina.net/heweipo/blog/337581
總結
以上是生活随笔為你收集整理的Spring MVC Rest 学习 一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python学习笔记三 pickle序
- 下一篇: win8.1注册表-修改资源管理器的默认