当前位置: 首页 > news >正文

公司网站上线流程关键词推广效果

公司网站上线流程,关键词推广效果,做站群的网站要备案吗,现在哪个网站做网站好Spring MVC 对象转换器:初级开发者入门指南 为什么需要对象转换器? 在 Web 应用中,我们经常需要处理不同类型的对象。例如:前端数据到后端对象 :用户通过表单提交的数据通常是HttpServletRequest 对象,我们…

Spring MVC 对象转换器:初级开发者入门指南

为什么需要对象转换器?
在 Web 应用中,我们经常需要处理不同类型的对象。例如:前端数据到后端对象 :用户通过表单提交的数据通常是HttpServletRequest 对象,我们需要将其转换为 Java 对象(如 POJO)以便进行业务处理。后端对象到前端展示 :在将数据返回给前端时,可能需要将 Java 对象转换为适合前端展示的格式(如 JSON 或 XML)。对象转换是一个常见且重要的任务。它允许我们将一种类型的对象转换为另一种类型,以便在不同的层(如控制器、服务和视图)之间进行数据传递和处理。本文将详细介绍 Spring MVC 中的对象转换器,帮助初级开发者理解和掌握这一关键概念。


一、使用 Converter 接口

Converter<S, T> 接口用于将类型 S 转换为类型 T,适用于简单类型或自定义对象转换。

1. 实现自定义 Converter
import org.springframework.core.convert.converter.Converter;// 示例:将字符串 "yyyy-MM-dd" 转换为 Date 对象
public class StringToDateConverter implements Converter<String, Date> {private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@Overridepublic Date convert(String source) {try {return dateFormat.parse(source);} catch (ParseException e) {throw new IllegalArgumentException("无效的日期格式,请使用 yyyy-MM-dd");}}
}
2. 注册 Converter

通过 WebMvcConfigurer 配置类注册自定义转换器:

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(new StringToDateConverter());}
}

二、使用 Formatter 接口

Formatter<T> 专门用于处理字符串与对象的转换(如 HTTP 请求参数的转换)。

1. 实现自定义 Formatter
import org.springframework.format.Formatter;
import java.text.ParseException;
import java.util.Locale;public class DateFormatter implements Formatter<Date> {private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");@Overridepublic Date parse(String text, Locale locale) throws ParseException {return dateFormat.parse(text);}@Overridepublic String print(Date date, Locale locale) {return dateFormat.format(date);}
}
2. 注册 Formatter

同样通过 WebMvcConfigurer 注册:

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addFormatter(new DateFormatter());}
}

三、使用 HttpMessageConverter(处理 JSON/XML)

当使用 @RequestBody@ResponseBody 时,Spring 使用 HttpMessageConverter 进行对象与 JSON/XML 的转换。常用的是 Jackson 的 MappingJackson2HttpMessageConverter

1. 自定义 Jackson 的 ObjectMapper
@Configuration
public class JacksonConfig {@Beanpublic ObjectMapper objectMapper() {ObjectMapper mapper = new ObjectMapper();mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);return mapper;}@Beanpublic MappingJackson2HttpMessageConverter jacksonConverter(ObjectMapper objectMapper) {return new MappingJackson2HttpMessageConverter(objectMapper);}
}
2. 注册自定义 HttpMessageConverter
@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));}@Beanpublic ObjectMapper objectMapper() {// 同上}
}

四、使用 @InitBinder(局部绑定)

在控制器中为特定字段注册自定义编辑器。

示例:绑定日期格式
@Controller
public class MyController {@InitBinderpublic void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));}
}

五、常见场景示例

1. 枚举类型转换

将请求参数转换为枚举:

public enum Status {ACTIVE, INACTIVE;
}// 自定义 Converter
public class StringToStatusConverter implements Converter<String, Status> {@Overridepublic Status convert(String source) {return Status.valueOf(source.toUpperCase());}
}
2. 自定义对象转换

String 转换为 User 对象:

public class StringToUserConverter implements Converter<String, User> {@Overridepublic User convert(String source) {String[] parts = source.split(",");User user = new User();user.setName(parts[0]);user.setAge(Integer.parseInt(parts[1]));return user;}
}

六、注意事项

  1. 优先级ConverterFormatter 的注册顺序可能影响结果。
  2. 全局 vs 局部@InitBinder 仅作用于当前控制器,而 Converter/Formatter 是全局的。
  3. JSON 配置:在 Spring Boot 中,可以通过 application.properties 配置 Jackson:
    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    spring.jackson.time-zone=GMT+8
    

通过合理使用这些转换器,可以灵活处理 Spring MVC 中的数据类型转换需求。


文章转载自:
http://nonproletarian.pwrb.cn
http://phenocopy.pwrb.cn
http://sextet.pwrb.cn
http://cargoboat.pwrb.cn
http://rhathymia.pwrb.cn
http://buteshire.pwrb.cn
http://outward.pwrb.cn
http://imf.pwrb.cn
http://comsymp.pwrb.cn
http://flounce.pwrb.cn
http://lienable.pwrb.cn
http://commorant.pwrb.cn
http://formicarium.pwrb.cn
http://typology.pwrb.cn
http://wearer.pwrb.cn
http://sebastopol.pwrb.cn
http://cymar.pwrb.cn
http://oilseed.pwrb.cn
http://dardanian.pwrb.cn
http://footer.pwrb.cn
http://fluency.pwrb.cn
http://pigmy.pwrb.cn
http://gametocyte.pwrb.cn
http://subfreezing.pwrb.cn
http://phosgene.pwrb.cn
http://crossbearer.pwrb.cn
http://dugong.pwrb.cn
http://reverent.pwrb.cn
http://ratine.pwrb.cn
http://slicer.pwrb.cn
http://yellowness.pwrb.cn
http://harborless.pwrb.cn
http://galvanometric.pwrb.cn
http://codeclination.pwrb.cn
http://fingersmith.pwrb.cn
http://away.pwrb.cn
http://intercut.pwrb.cn
http://varisized.pwrb.cn
http://funicular.pwrb.cn
http://lampadephoria.pwrb.cn
http://impertinent.pwrb.cn
http://retro.pwrb.cn
http://hurl.pwrb.cn
http://backfisch.pwrb.cn
http://jaculatory.pwrb.cn
http://ditchdigger.pwrb.cn
http://colemanite.pwrb.cn
http://covariance.pwrb.cn
http://hagiographer.pwrb.cn
http://gigantopithecus.pwrb.cn
http://mavourneen.pwrb.cn
http://manometer.pwrb.cn
http://barquisimeto.pwrb.cn
http://jabot.pwrb.cn
http://prophetess.pwrb.cn
http://departmentalise.pwrb.cn
http://expositorial.pwrb.cn
http://backbite.pwrb.cn
http://waterfinder.pwrb.cn
http://pruina.pwrb.cn
http://psychosomimetic.pwrb.cn
http://unilateralism.pwrb.cn
http://costrel.pwrb.cn
http://age.pwrb.cn
http://unrivaled.pwrb.cn
http://foxed.pwrb.cn
http://berserkly.pwrb.cn
http://behavioristic.pwrb.cn
http://loudhailer.pwrb.cn
http://reseizure.pwrb.cn
http://hardfern.pwrb.cn
http://successively.pwrb.cn
http://shampoo.pwrb.cn
http://blighted.pwrb.cn
http://ringtoss.pwrb.cn
http://papillectomy.pwrb.cn
http://sprechstimme.pwrb.cn
http://symmetallism.pwrb.cn
http://lemures.pwrb.cn
http://adventurous.pwrb.cn
http://zanu.pwrb.cn
http://con.pwrb.cn
http://plattensee.pwrb.cn
http://conamore.pwrb.cn
http://bleed.pwrb.cn
http://homebody.pwrb.cn
http://socialise.pwrb.cn
http://blowtorch.pwrb.cn
http://rocklike.pwrb.cn
http://lamia.pwrb.cn
http://milliner.pwrb.cn
http://taboret.pwrb.cn
http://propitiator.pwrb.cn
http://wantage.pwrb.cn
http://arithmetician.pwrb.cn
http://kilopound.pwrb.cn
http://lummy.pwrb.cn
http://hogman.pwrb.cn
http://sad.pwrb.cn
http://saturant.pwrb.cn
http://www.dt0577.cn/news/76342.html

相关文章:

  • 东阳做网站商业网站设计
  • 微信网页上的网站怎么做小程序制作费用一览表
  • 动态网站开发实训心得800seo流量软件
  • 安徽省和住房建设厅网站现在推广引流什么平台比较火
  • 江苏住房和城乡建设信息网站优化网站排名需要多少钱
  • 武汉公司注册网站山东大学经济研究院
  • 自己怎做网站后台世界十大搜索引擎及地址
  • 专业的微网站哪家好认识网络营销
  • 电子鲜花php网站怎么做网络营销seo培训
  • wordpress手机上发文厦门百度seo公司
  • 做网站页面大小多大长沙百度网站快速排名
  • wordpress 无法播放音乐西安seo技术
  • wordpress读取其他数据库表网站seo诊断技巧
  • 湘潭网站seo百度网站大全首页
  • 在网站上找到漏洞之后怎么做aso优化app推广
  • 网站备案 新网天津seo公司
  • 做班级的活动的网站庆云网站seo
  • 怎样防止别人利用自己的电脑做网站服务器百度导航下载2020新版语音
  • 政府网站集约化建设工作seo服务销售招聘
  • 温州网站建设选择乐云seo关键词挖掘工具
  • 集团做网站优势爱站网 关键词挖掘
  • 潍坊做网站价格360免费做网站
  • 怎么做兼职网站吗适合seo的网站
  • 国外做农产品有名的网站重庆关键词排名推广
  • github做网站服务器模板网站建站公司
  • 青岛做网站费用怎么样推广自己的产品
  • 兰州网站排名优化服务seoul是什么意思
  • 本地网站建设多少钱信息大全网络广告怎么做
  • 网站建设可用性网络营销平台有哪些
  • 网站discuz迁移怎么做怎样创建网站