加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

springmvc rest服务deom实例

发布时间:2020-12-15 03:20:52 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ?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/XMLSc

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    <?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,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,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,@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你学会使用了吗!

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读