springmvc rest服务deom实例

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

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

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


开发过程中是不可避免地会出现各种异常情况的,例如网络连接异常、数据格式异常、空指针异常等等。异常的出现可能导致程序的运行出现问题,甚至直接导致程序崩溃。因此,在开发过程中,合理处理异常、避免异常产生、以及对异常进行有效的调试是非常重要的。 对于异常的处理,一般分为两种方式: 编程式异常处理:是指在代
说明:使用注解方式实现AOP切面。 什么是AOP? 面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 通俗描述:不通过修改源代码方式,在主干功能里面添加新功能。 AOP底层使用动态代理。 AOP术语 连接点
Spring MVC中的拦截器是一种可以在请求处理过程中对请求进行拦截和处理的机制。 拦截器可以用于执行一些公共的操作,例如日志记录、权限验证、数据转换等。在Spring MVC中,可以通过实现HandlerInterceptor接口来创建自定义的拦截器,并通过配置来指定拦截器的应用范围和顺序。 S
在 JavaWeb 中,共享域指的是在 Servlet 中存储数据,以便在同一 Web 应用程序的多个组件中进行共享和访问。常见的共享域有四种:ServletContext、HttpSession、HttpServletRequest、PageContext。 ServletContext 共享域:
文件上传 说明: 使用maven构建web工程。 使用Thymeleaf技术进行服务器页面渲染。 使用ResponseEntity实现下载文件的功能。 @Controller public class FileDownloadAndUpload { @GetMapping(&quot;/file/d
创建初始化类,替换web.xml 在Servlet3.0环境中,Web容器(Tomcat)会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类,如果找到的话就用它来配置Servlet容器。 Spring提供了这个接口的实现,名为SpringS
在 Web 应用的三层架构中,确保在表述层(Presentation Layer)对数据进行检查和校验是非常重要的。正确的数据校验可以确保业务逻辑层(Business Logic Layer)基于有效和合法的数据进行处理,同时将错误的数据隔离在业务逻辑层之外。这有助于提高系统的健壮性、安全性和可维护
什么是事务? 事务(Transaction)是数据库操作最基本单元,逻辑上一组操作,要么都成功,要么都失败,如果操作之间有一个失败所有操作都失败 。 事务四个特性(ACID) 原子性 一组操作要么都成功,要么都失败。 一致性 一组数据从事务1合法状态转为事务2的另一种合法状态,就是一致。 隔离性 事
什么是JdbcTemplate? Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作。 准备工作 引入jdbcTemplate的相关依赖: 案例实操 创建jdbc.properties文件,配置数据库信息 jdbc.driver=com.mysql.cj.
SpringMVC1.MVC架构MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是一种软件设计规范是将业务逻辑、数据、显示分离的方法来写代码MVC主要作用是:降低了视图和业务逻辑之间的双向耦合MVC是一个架构模型,不是一种设计模式。1.model(模型)数据模型,提供要展示的数据,因此包
SpringMVC学习笔记1.SpringMVC应用1.1SpringMVC简介​SpringMVC全名叫SpringWebMVC,是⼀种基于Java的实现MVC设计模型的请求驱动类型的轻量级Web框架,属于SpringFrameWork的后续产品。​MVC全名是ModelViewController,是模型(model)-视图(view)-控制器(co
11.1数据回显基本用法数据回显就是当用户数据提交失败时,自动填充好已经输入的数据。一般来说,如果使用Ajax来做数据提交,基本上是没有数据回显这个需求的,但是如果是通过表单做数据提交,那么数据回显就非常有必要了。11.1.1简单数据类型简单数据类型,实际上框架在这里没有
一、SpringMVC简介1、SpringMVC中重要组件DispatcherServlet:前端控制器,接收所有请求(如果配置/不包含jsp)HandlerMapping:解析请求格式的.判断希望要执行哪个具体的方法.HandlerAdapter:负责调用具体的方法.ViewResovler:视图解析器.解析结果,准备跳转到具体的物
1.它们主要负责的模块Spring主要应用于业务逻辑层。SpringMVC主要应用于表现层。MyBatis主要应用于持久层。2.它们的核心Spring有三大核心,分别是IOC(控制反转),DI(依赖注入)和AOP(面向切面编程)。SpringMVC的核心是DispatcherServlet(前端控制器)。MyBatis的核心是ORM(对
3.注解开发Springmvc1.使用注解开发要注意开启注解支持,2.注解简化了,处理映射器和处理适配器,只用去管视图解析器即可案例代码:1.web.xml,基本不变可以直接拿去用<!--调用DispatcherServlet--><servlet><servlet-name>springmvc</servlet-name>
拦截器概述SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。**过滤器与拦截器的区别:**拦截器是AOP思想的具体应用。过滤器servlet规范中的一部分,任何javaweb工程都可以使用
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="
学习内容:1、SSH&SSM2、Spring3、Struts2&SpringMVC4、Hibernate&MyBatis学习产出:1.SSH和SSM都是有Spring框架的,他们两个差不多。2.Spring分为四个模块,持久层,表示层,检测层,还有核心层,核心层分为2个关键核心功能。分别为,控制反转(IOC),依赖注入(DI),和面向切面编程
一、SpringMVC项目无法引入js,css的问题具体原因是css和js等被SpringMVC拦截了:解决方案:在spring-mvc.xml中配置<mvc:default-servlet-handler/><?xmlversion="1.0"encoding="UTF-8"?><beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
开发环境:Eclipse/MyEclipse、Tomcat8、Jdk1.8数据库:MySQL前端:JavaScript、jQuery、bootstrap4、particles.js后端:maven、SpringMVC、MyBatis、ajax、mysql读写分离、mybatis分页适用于:课程设计,毕业设计,学习等等系统介绍