首页 > 其他分享 >Rest接口和Thymeleaf的两个坑

Rest接口和Thymeleaf的两个坑

时间:2022-12-19 10:36:18浏览次数:31  
标签:www http spring springframework 接口 Thymeleaf Rest org schema

spring boot thymeleaf 热部署

在使用spring boot 开发的时候,使用了Thymeleaf 作为前端的模板开发,发现在调试过程中,改动了Thymeleaf模板后,需要重新启动下项目,才可以立即生效

解决办法:

ctrl+shift+f9

Rest接口和Thymeleaf的两个坑_xml


spring boot推荐支持,因为spring boot是快速开发,而thymeleaf又是原型即页面,所以从理念是接近的 

单纯从效率上看,没有什么优势,而且用这种测试也不太准
thymeleaf 的首次渲染比Beetl差的是数量级,后续的持续渲染,3.0版本是有很大提升的,也和Beetl也差不多
优势是 html 的显示优势 前后端可以很好的分离,要是有很多的页面拆分(include 的部分)优势也不是很明显了

是的,基本上都会抽出一些公用模版,所以这种优势并不明显,像引入的js,css

 

 

 

如果配置了HttpMessageConverter,然后在@ResponseBody的接口的返回值使用JSON.toJSONString()转换过,输出的结果就会有以下提示

解决办法:接口直接返回对象,Spring会调用已经配置的HttpMessageConverter将对象转换成json字符串

 

Rest接口和Thymeleaf的两个坑_xml_02


 

HttpMessageConvert配置示例:

<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<list>
<value>AllowArbitraryCommas</value>
<value>AllowUnQuotedFieldNames</value>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"></property>
</bean>
</property>
</bean>


<?xml version="1.0" encoding="UTF-8"?>
<!-- 注意!SpringMVC的配置文件使用的是mvc命名空间 -->
<beans:beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.mkyong.common.controller" />

<annotation-driven>
<message-converters register-defaults="true">
<!-- @ResponseBody乱码问题,将StringHttpMessageConverter的默认编码设为UTF-8 -->
<beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:constructor-arg value="UTF-8"/>
</beans:bean>
<!-- 配置Fastjson支持 -->
<beans:bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<beans:property name="charset" value="UTF-8"/>
<beans:property name="supportedMediaTypes">
<beans:list>
<beans:value>application/json</beans:value>
<beans:value>text/html;charset=UTF-8</beans:value>
</beans:list>
</beans:property>
<beans:property name="features">
<beans:list>
<beans:value>WriteMapNullValue</beans:value>
<beans:value>QuoteFieldNames</beans:value>
<beans:value>WriteDateUseDateFormat</beans:value>
<beans:value>WriteEnumUsingToString</beans:value>
</beans:list>
</beans:property>
</beans:bean>
</message-converters>
</annotation-driven>
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/"/>
<beans:property name="suffix" value=".jsp"/>
</beans:bean>
</beans:beans>

标签:www,http,spring,springframework,接口,Thymeleaf,Rest,org,schema
From: https://blog.51cto.com/u_15147537/5951464

相关文章