springmvc rest服務deom實例
服務實現功能如下:
spring rest服務:
1.GetClient和GetController,Spring mvc rest template的get方法使用demo.
2.PostClient和PostController,Spring mvc rest template的post方法使用demo.
3.FileUploadClient和FileUploadController,Spring mvc rest template向服務器上傳文件demo.
閑話少說,這是一個web服務,所以先看web.xml,
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!-- 配置spring mvc的主控servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet><!-- 配置spring mvc處理的url --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.mvc</url-pattern> </servlet-mapping> </web-app> </pre>
然后看,springmvc對應的配置文件:<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- spring mvc需要一個自己的配置文件,此配置文件的名字和web.xml文件中關于spring mvc相關的servlet的 servlet name有一個契約,必須采用<servlet-name>-servlet.xml --> <!-- 掃描controller包, 將標注spring注解的類自動轉化為bean, 同時完成bean的注入 --> <context:component-scan base-package="com.ilucky.springmvc.rest.controller" /> <!-- 配置springmvc視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.JstlView</value> </property> <property name="prefix"> <value>/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- 設置上傳文件的最大尺寸為100MB --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>102400000</value> </property> </bean>
</beans> </pre>
馬上看get實例:package com.ilucky.springmvc.rest.controller;import org.springframework.web.client.RestTemplate; /** * 測試Spring Rest的Get方法. * @author IluckySi * @since 20141107 */ public class GetClient { public static void main (String[] args) { String url = "http://localhost:8080/spring-rest/get.mvc?test=get"; RestTemplate rest = new RestTemplate(); Object result = rest.getForObject(url, String.class); System.out.println("測試Spring Rest的Get方法: " + result); } } </pre>
package com.ilucky.springmvc.rest.controller;import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * 測試Spring Rest的Get方法. * @author IluckySi * @since 20141107 */ @Controller("getController") @Scope("prototype") public class GetController { @RequestMapping(value = "/get", method = RequestMethod.GET) public void get(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "test", required = true) String test) { System.out.println("測試Spring Rest的Get方法: test = " + test); PrintWriter pw = null; try { response.setContentType("application/xml;charset=utf-8"); pw = response.getWriter(); pw.write("<result>success</result>"); } catch (Exception e) { e.printStackTrace(); } finally { pw.close(); } } } </pre>
post實例:package com.ilucky.springmvc.rest.controller;import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; /** * 測試Spring Rest的Post方法. * @author IluckySi * @since 20141107 */ public class PostClient { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main (String[] args) { String url = "http://localhost:8080/spring-rest/post.mvc"; RestTemplate rest = new RestTemplate(); MultiValueMap<String, Object> param = new LinkedMultiValueMap(); param.add("test", "post"); Object result = rest.postForObject(url, param, String.class); System.out.println("測試Spring Rest的Post方法: " + result); } } </pre>
package com.ilucky.springmvc.rest.controller;import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * 測試Spring Rest的Post方法. * @author IluckySi * @since 20141107 */ @Controller("postController") @Scope("prototype") public class PostController { @RequestMapping(value = "/post", method = RequestMethod.POST) public void post(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "test", required = true) String test) { System.out.println("測試Spring Rest的Post方法: test = " + test); PrintWriter pw = null; try { response.setContentType("application/xml;charset=utf-8"); pw = response.getWriter(); pw.write("<result>success</result>"); } catch (Exception e) { e.printStackTrace(); } finally { pw.close(); } } } </pre>
上傳文件實例:package com.ilucky.springmvc.rest.controller;import java.io.File; import org.springframework.core.io.FileSystemResource; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; /** * 測試Spring Rest的文件上傳. * @author IluckySi * @since 20141107 * 注意:使用Spring Rest進行文件上傳, * 需要在spring mvc對應的配置文件中,做如下配置: * <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>102400000</value> </property> </bean> 否則會報如下異常: resulted in 400 (Bad Request); invoking error handler */ public class FileUploadClient { @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) { String url = "http://localhost:8080/spring-rest/fileUpload.mvc"; String filePath = "E:\\fileUpload.zip"; RestTemplate rest = new RestTemplate(); FileSystemResource resource = new FileSystemResource(new File(filePath)); MultiValueMap<String, Object> param = new LinkedMultiValueMap(); param.add("fileName", "fileUpload.zip"); param.add("file", resource); Object result = rest.postForObject(url, param, String.class); System.out.println("測試Spring RestT的文件上傳: " + result); } } </pre>
package com.ilucky.springmvc.rest.controller;import java.io.File; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; /** * 測試Spring Rest的文件上傳. * @author IluckySi * @since 20141107 */ @Controller("fileUploadController") @Scope("prototype") public class FileUploadController { @RequestMapping(value = "/fileUpload", method = RequestMethod.POST) public void fileUpload(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "fileName", required = true) String fileName, @RequestParam(value = "file", required = true) MultipartFile file) { System.out.println("文件名稱是: " + fileName); PrintWriter pw = null; try { //將文件寫入目標文件,如果有文件服務器,可以將其寫到固定的文件服務器上,測試:寫到本地文件. File server = new File("E://" + fileName); file.transferTo(server); response.setContentType("application/xml;charset=utf-8"); pw = response.getWriter(); pw.write("<result>success</result>"); System.out.println("測試Spring Rest的文件上傳"); } catch (Exception e) { e.printStackTrace(); } finally { pw.close(); } } } </pre>
ok! 完工!spring mvc 的rest你學會使用了嗎!
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!