服务实现功能如下:
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>
然后看,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>
马上看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); } }
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(); } } }
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); } }
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(); } } }
上传文件实例:
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); } }
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(); } } }
ok! 完工!spring mvc 的rest你学会使用了吗!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/8007.html