Rest接口和Thymeleaf的两个坑

简介: spring boot thymeleaf 热部署 在使用spring boot 开发的时候,使用了Thymeleaf 作为前端的模板开发,发现在调试过程中,改动了Thymeleaf模板后,需要重新启动下项目,才可以立即生效解决办法:ctrl+shift+f9  http://www.

spring boot thymeleaf 热部署

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

 http://www.oschina.net/question/779083_2148086


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

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

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

 

 

 

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

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

 

另外一个解决地思路:http://blog.csdn.net/u010161082/article/details/46618947

 

 

 

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>

http://blog.csdn.net/my_god_sky/article/details/53385246

<?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>

 

http://www.cnblogs.com/sunp823/p/5601397.html

 

相关文章
|
4月前
|
JSON 前端开发 API
Apache HttpClient调用Spring3 MVC Restful Web API演示
Apache HttpClient调用Spring3 MVC Restful Web API演示
41 1
|
4月前
|
JSON Java API
使用Spring Boot实现RESTful API
使用Spring Boot实现RESTful API
|
4月前
|
Java API Spring
Spring Boot中配置Swagger用于API文档
Spring Boot中配置Swagger用于API文档
|
4月前
|
Java API 网络架构
【Spring Boot】详解restful api
【Spring Boot】详解restful api
71 0
|
4月前
|
XML 前端开发 Java
Spring3 MVC中使用Swagger生成API文档
Spring3 MVC中使用Swagger生成API文档
41 0
BXA
|
JSON 前端开发 安全
基于Spring的RESTful API开发
RESTful API是一种建立在HTTP协议之上的Web API。它遵循REST设计规范使用HTTP请求通过网络进行通信,实现对资源的增、删、改、查等操作。RESTful API与传统的Web API相比更具有灵活性、可扩展性与可维护性,已经成为现代Web服务的首选技术之一。
BXA
108 0
|
5月前
|
缓存 Java 关系型数据库
Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)
Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)
152 0
|
前端开发 Java API
# Spring MVC与RESTful API:如何设计高效的Web接口
# Spring MVC与RESTful API:如何设计高效的Web接口
110 0
BXA
|
缓存 前端开发 IDE
Spring MVC实现RESTful API
Spring MVC是Spring Framework的一个模块,它为Web应用程序提供了一种模型-视图-控制器(MVC)架构。通过分离模型(Model)、视图(View)和控制器(Controller)的方式,使代码易于维护和测试。同时Spring MVC还支持RESTful API的开发
BXA
122 0
|
NoSQL 前端开发 Java
Spring-Data-REST轻松搞定RESTfulAPI
昨天同事问我有没有研究过 spring-boot-starter-data-rest,没有~但是看名字就大概知道是做什么的(命名的重要性),因为之前有了解过 spring-boot-starter-data-jpa ,过一会发过两个截图过来。真的很强大,感觉这个在使用RESTful风格接口协议的微服务时都不用写Controller了。