Springmvc请求参数类型转换器及原生api代码实例

一、springmvc的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
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--扫描组件-->
  <context:component-scan base-package="com.wuxi"></context:component-scan>
  <!--视图解析器-->
  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
  <!--参数类型装换器-->
  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="com.wuxi.utils.StringToDateConverter"></bean>
      </set>
    </property>
  </bean>
  <!--开启springmvc框架注解的支持-->
  <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>

二、转换的类

package com.wuxi.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {
  @Override
  public Date convert(String string) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
      date = sdf.parse(string);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }
}

三、接口

@RequestMapping("/student")
public String student(Student student, HttpServletRequest request, HttpServletResponse response) {
  System.out.println(student);
  return "success";
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • SpringMVC的@InitBinder参数转换代码实例

    这篇文章主要介绍了SpringMVC的@InitBinder参数转换代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 @Controller @RequestMapping("/index") public class IndexController { /** * 解决前端传递的日期参数验证异常 * * @param binder * @author hzj */ @InitBinder({"param",

  • 解决springmvc关于前台日期作为实体类对象参数类型转换错误的问题

    页面报错: 后台错误: Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessag

  • Springmvc自定义参数转换实现代码解析

    springmvc的参数绑定有以下几种方法: 1)默认的参数绑定 Request Response Session Model(实现ModelMap) 2)简单类型参数绑定 方法的形参上(Integer id,String,Double,Boolean) 3)pojo类型 4)包装类型 QueryVo 5)参数绑定之自定义参数转换 高级参数绑定 1)绑定数组 直接在方法的参数上绑定 xxx[] xxx 将数组注入对象,用该对象来接受数组 2)绑定list 使用包装类,包装类中有list集合 自定

  • SpringBoot中通过实现WebMvcConfigurer参数校验的方法示例

    在Spring5.0和SpringBoot2.0中废弃了WebMvcConfigurerAdapter类. 现有两种解决方案 1 直接实现WebMvcConfigurer (官方推荐) 2 直接继承WebMvcConfigurationSupport 本篇文章讨论下使用第一种方式完成参数校验. 首先附上代码. @Slf4j @Controller @RequestMapping("/goods") public class GoodsController { @Autowired Mi

  • 快速解决SpringMVC @RequestBody 用map接收请求参数的问题

    一:遇到个跨域调用,因为传个我的参数不定,所以需要通过map来接收参数并进行签名验证等操作 理所当然的写出了下面的代码,但是发现map里并没有获取到传来的key-value值 @RequestMapping(value = "/callback", produces = "text/html;charset=UTF-8") @ResponseBody public String callback(@RequestBody Map<String, String&

  • SpringMVC Controller解析ajax参数过程详解

    在使用ajax发送请求时,如果发送的JSON数据的参数是一个类中的不同属性,在Controller类的方法中使用@RequestBody Object obj会直接封装进obj对象中 例如: 前端部分代码 JavaScript <script language="JavaScript"> function login(){ var user_name = $("#user_name").val(); //用户名 var user_password = $

  • 详解在Spring MVC或Spring Boot中使用Filter打印请求参数问题

    使用Spring MVC或Spring Boot中打印或记录日志一般使用AOP记录Request请求和Response响应参数,在不使用AOP的前提下,如果在Filter中打印日志,在打印或消费请求类型为Content-Type:application/json的请求时,会出现严重的问题. 在Spring体系中,过滤器的定义我们一般采用继承OncePerRequestFilter的方式,当然也可以使用原始的Filter. 错误写法一: 如果不对request和response进行处理,使用伪代码

  • SpringMvc自动装箱及GET请求参数原理解析

    在我的概念里边,GET请求需要加上注解@RequestParam,然后它的参数类型只能是 基本数据类型 或者 基本数据类型的包装类,比如:@RequestParam String name(默认是必传的),也可以不加@RequestParam 注解,其实就相当于@RequestParam(required = false) 但是参数类型竟然是自定义对象,对象类里有不同的参数和get/set方法,而且没有使用@RequestParam 注解,那么同样也能实现GET请求 比如一个请求方法是:publ

  • Springmvc请求参数类型转换器及原生api代码实例

    一.springmvc的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.spr

  • SpringMVC请求参数的使用总结

    本次数据请求使用postman, postman下载地址:https://www.getpostman.com/ 一.页面跳转 1. 页面跳转 @Controller public class IndexController { /** * 进入首页 * * @return 首页页面 */ @RequestMapping("/") public String index(){ return "/index"; } } 2. 请求转发 @Controller publ

  • mybatis自定义参数类型转换器数据库字段加密脱敏

    目录 1 问题背景 2 解决方案 2.1 使用数据库加密算法 2.2 使用mybatis的自定义参数类型转换器 3 一般web项目使用 3.1 创建自定义Java类型 3.2 自定义类的转换处理器 3.3 配置自定义类型和类型转换器 3.4 查询使用 3.5 新增修改使用 4. springboot项目使用 4.1 配置自定义类型和类型转换器 4.2 mybatis-plus的使用 1 问题背景 在数据库存储人员的信息时,有一些信息是敏感数据,如身份证号.出生地等.为了防止信息泄漏,这些信息不允

  • java 用泛型参数类型构造数组详解及实例

    java 用泛型参数类型构造数组详解及实例 前言: 前一阵子打代码的时候突然想到一个问题.平时我们的数组都是作为一个参数传入方法中的,如果我们要想在方法中创建一个数组怎么样呢?在类型明确的情况下,这是没什么难度的.如果我们传入的参数是泛型类型的参数呢? public static <T> T[] creArray (T obj){ T[] arr = new T[10]; } 像上面这种用T来直接new数组的方法是错误的,会编译时出现一个:Cannot create a generic arr

  • Vue请求java服务端并返回数据代码实例

    这篇文章主要介绍了Vue请求java服务端并返回数据代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 最近在自学vue怎么与java进行数据交互.其实axios还是挺简单的,与ajax请求几乎一样,无外乎也就是要解决下跨域的问题. 废话不多说了,直接贴代码,一看就懂! //向springmvc Controller发起请求,传递一个参数 get请求(带参数传递) axios.get('http://127.0.0.1:8088/inas/

  • 微信小程序封装多张图片上传api代码实例

    这篇文章主要介绍了微信小程序封装多张图片上传api代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 export default class Upload{ constructor(object) { this.obj = { count:1, sizeType:['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType:['album','camera'], //

  • vue使用原生swiper代码实例

    这篇文章主要介绍了vue使用原生swiper代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 <template> <div> <div class="swiper_Box" :class="identify"> <div class="swiper-wrapper" :ref="identify"> <d

  • 多种类型jQuery网页验证码插件代码实例

    html <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,m

  • SpringMVC实现自定义类型转换器

    我们在使用SpringMVC时,常常需要把表单中的参数映射到我们对象的属性中,我们可以在默认的spring-servlet.xml加上如下的配置即可做到普通数据类型的转换,如将String转换成Integer和Double等: <mvc:annotation-driven /> 或 复制代码 代码如下: <bean id="conversionService" class="org.springframework.format.support.Formatt

  • springmvc实现自定义类型转换器示例

    springmvc除了自带的部分类型转换之外,还可以自定义类型转换器,按照以下步骤: 1.写一个类实现Converter接口 package com.hy.springmvc.entities; import org.springframework.core.convert.converter.Converter; import com.google.gson.Gson; public class DepartmentConvertor implements Converter<String,

随机推荐