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

spring MVC自定义视图实现jsonp

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

约定的优先级规则:

Spring supports a couple of conventions for selecting the format required: URL suffixes and/or a URL parameter. These work alongside the use of Accept headers. As a result, the content-type can be requested in any of three ways. By default they are checked in this order:
Add a path extension (suffix) in the URL. So, if the incoming URL is something likehttp://myserver/myapp/accounts/list.html then HTML is required. For a spreadsheet the URL should be http://myserver/myapp/accounts/list.xls. The suffix to media-type mapping is automatically defined via the JavaBeans Activation Framework or JAF (so activation.jar must be on the class path).
A URL parameter like this: http://myserver/myapp/accounts/list?format=xls. The name of the parameter is format by default, but this may be changed. Using a parameter is disabled by default, but when enabled, it is checked second.
Finally the Accept HTTP header property is checked. This is how HTTP is actually defined to work, but, as previously mentioned, it can be problematic to use.

spring-web.xml配置:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>

<!--1、检查扩展名(如my.pdf);2、检查Parameter(如my?format=pdf);3、检查Accept Header-->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
    <property name="favorPathExtension" value="true"/>
    <!-- 用于开启 /userinfo/123?format=json 的支持 -->
    <property name="favorParameter" value="true"/>
    <property name="parameterName" value="format"/>
    <!-- 是否忽略Accept Header -->
    <property name="ignoreAcceptHeader" value="false"/>
    <!-- 扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 注:此项由默认后缀匹配 -->
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
            <entry key="html" value="text/html"/>
        </map>
    </property>
    <!-- 默认的content type -->
    <property name="defaultContentType" value="application/json"/>
</bean>

<!-- 内容协商视图解析器;根据客户端不同的请求决定不同的view进行响应 -->
<!-- 会自动根据解析的contentType来决定使用哪个视图解析器(默认使用整个web应用中的viewResolver) -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <!-- 内容协商管理器 用于决定media type -->
    <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
    <!-- 默认视图 放在解析链最后 -->
    <property name="defaultViews">
        <list>
            <!--<bean class="com.alibaba.fastjson.support.spring.FastJsonJsonView"/>-->
            <bean class="com.caiya.test.ExtendedJsonView"/>
        </list>
    </property>
    <property name="order" value="0"/>
</bean>

<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用 html)- -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

json和jsonp视图类:

package com.caiya.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.view.AbstractView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by caiya on 16/2/1.
 */
public class ExtendedJsonView extends AbstractView {

    public static final String  DEFAULT_CONTENT_TYPE = "application/json";

    public final static Charset UTF8                 = Charset.forName("UTF-8");

    private Charset             charset              = UTF8;

    private SerializerFeature[] serializerFeatures    = new SerializerFeature[0];

    private Set<String> renderedAttributes;

    private boolean             disableCaching       = true;

    private boolean             updateContentLength  = false;

    public ExtendedJsonView(){
        setContentType(DEFAULT_CONTENT_TYPE);
        setExposePathVariables(false);
    }

    public void setRenderedAttributes(Set<String> renderedAttributes) {
        this.renderedAttributes = renderedAttributes;
    }

    @Deprecated
    public void setSerializerFeature(SerializerFeature... features) {
        this.setFeatures(features);
    }

    public Charset getCharset() {
        return this.charset;
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }

    public SerializerFeature[] getFeatures() {
        return serializerFeatures;
    }

    public void setFeatures(SerializerFeature... features) {
        this.serializerFeatures = features;
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
                                           HttpServletResponse response) throws Exception {
        Object value = filterModel(model);

        String text = JSON.toJSONString(value, serializerFeatures);

        String callback = request.getParameter("callback");
        if(StringUtils.isNotBlank(callback)){
            text = new StringBuilder(callback).append("(").append(text).append(")").toString();
        }

        byte[] bytes = text.getBytes(charset);

        OutputStream stream = this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream();
        stream.write(bytes);

        if (this.updateContentLength) {
            writeToResponse(response, (ByteArrayOutputStream) stream);
        }
    }

    @Override
    protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
        setResponseContentType(request, response);
        response.setCharacterEncoding(UTF8.name());
        if (this.disableCaching) {
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache, no-store, max-age=0");
            response.addDateHeader("Expires", 1L);
        }
    }

    /**
     * Disables caching of the generated JSON.
     * <p>
     * Default is {@code true}, which will prevent the client from caching the generated JSON.
     */
    public void setDisableCaching(boolean disableCaching) {
        this.disableCaching = disableCaching;
    }

    /**
     * Whether to update the 'Content-Length' header of the response. When set to {@code true}, the response is buffered
     * in order to determine the content length and set the 'Content-Length' header of the response.
     * <p>
     * The default setting is {@code false}.
     */
    public void setUpdateContentLength(boolean updateContentLength) {
        this.updateContentLength = updateContentLength;
    }

    /**
     * Filters out undesired attributes from the given model. The return value can be either another {@link Map}, or a
     * single value object.
     * <p>
     * Default implementation removes {@link BindingResult} instances and entries not included in the
     * {@link #setRenderedAttributes(Set) renderedAttributes} property.
     *
     * @param model the model, as passed on to {@link #renderMergedOutputModel}
     * @return the object to be rendered
     */
    protected Object filterModel(Map<String, Object> model) {
        Map<String, Object> result = new HashMap<String, Object>(model.size());
        Set<String> renderedAttributes = !CollectionUtils.isEmpty(this.renderedAttributes) ? this.renderedAttributes : model.keySet();
        for (Map.Entry<String, Object> entry : model.entrySet()) {
            if (!(entry.getValue() instanceof BindingResult) && renderedAttributes.contains(entry.getKey())) {
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return result;
    }

}

测试controller:

package com.caiya.test.controllers;

import org.apache.log4j.Logger;
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.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by caiya on 16/1/28.
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {


    private static final Logger logger = Logger.getLogger(TestController.class);

    @RequestMapping(value = {"/test2.json"})
    public Object test2(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("key", "value");
        return map;
    }

    @RequestMapping(value = {"/test2"}, produces = {"application/json"})
    public Object test22(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("key", "value");
        return map;
    }

    @RequestMapping(value = {"/test2"}, produces = {"text/html"})
    public Object test222(){

        return "cookie";
    }


}

效果图:

160810_89ZC_576855.png

160810_ZCOo_576855.png

160811_TKKt_576855.png

161831_1Mns_576855.png

161832_xsr2_576855.png

161832_JCGJ_576855.png

--------------------------------------


转载于:https://my.oschina.net/wnjustdoit/blog/612638

相关文章:

  • 怎么提高ArcGIS for Desktop10.x的性能
  • python文件相关操作
  • socket.io+angular.js+express.js做个聊天应用(四)
  • BUG系列
  • openstack环境准备
  • MYSQL远程登录权限设置(转)
  • linux 下 NFS服务器配置
  • Spoj 2713 Can you answer these queries IV 水线段树
  • swap函數 进阶探讨与实现
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • Badboy - 从excel中读取数据
  • spring - ioc和aop
  • Java中内存分配
  • Android--获取系统时间的几种方式
  • PLSQL Developer过期要注冊表
  • ES6指北【2】—— 箭头函数
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • Asm.js的简单介绍
  • avalon2.2的VM生成过程
  • co模块的前端实现
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • java中的hashCode
  • learning koa2.x
  • Mac 鼠须管 Rime 输入法 安装五笔输入法 教程
  • ubuntu 下nginx安装 并支持https协议
  • vue-loader 源码解析系列之 selector
  • webgl (原生)基础入门指南【一】
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 从setTimeout-setInterval看JS线程
  • 分类模型——Logistics Regression
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 携程小程序初体验
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • Java数据解析之JSON
  • Semaphore
  • 阿里云API、SDK和CLI应用实践方案
  • 函数计算新功能-----支持C#函数
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • !!java web学习笔记(一到五)
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (6)STL算法之转换
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (分类)KNN算法- 参数调优
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET 8.0 中有哪些新的变化?
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • @31省区市高考时间表来了,祝考试成功
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)