Spring Boot 实现json和jsonp格式数据接口
2017-09-13 14:32
686 查看
Spring boot 实现json和jsonp格式数据接口
1.新建一个类继承AbstractJsonpResponseBodyAdvice,重写父类构造方法,传入callback和jsonp参数。
package com.alibaba.sinfo.h5.agent.advice; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; /** * Created by Jack on 2017/5/31. */ @ControllerAdvice public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice { public JsonpAdvice() { super("callback", "jsonp"); } }
2.写返回json和jsonp格式数据的Controller
package com.alibaba.sinfo.h5.agent.controller import com.alibaba.fastjson.JSONObject import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController import java.text.SimpleDateFormat /** * Created by jack on 2017/5/19. */ @RestController class HelloWorld { @GetMapping("/hello") def helloWorld(){ JSONObject object = new JSONObject() object.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) object } }
测试输出
//http://localhost:8500/hello { time: "2017-05-31 22:04:50" }
// 20170531220604
// http://localhost:8500/hello?callback=hellojsonp
/**/hellojsonp({
"time": "2017-05-31 22:06:03"
});
相关文章推荐
- Spring boot 实现json和jsonp格式数据,接口共用
- SpringMvc+ajax 实现json格式数据传递
- Spring Boot系列(三):Spring Boot转化为json数据格式
- Spring boot项目设定返回数据为JSON固定格式
- springboot 返回json格式数据的时间格式配置
- 【Java】SpringBoot 引入fastjson库进行json格式数据返回实例
- SpringMvc+ajax 实现json格式数据传递
- SpringMvc+ajax 实现json格式数据传递
- springboot 返回json格式数据时间格式配置
- Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(json web token) 用户发送按照约定,向服务端发送 Header、Payload 和 Signature,
- vs2015开发webserver 返回json格式 实现图书编目数据接口
- SpringBoot入门系列篇(二):SpringBoot发送json格式数据
- springboot 返回json格式数据时间格式配置
- Spring Boot 日期数据格式转换@JsonFormat实例
- 解决spring boot中rest接口404,500等错误返回统一的json格式
- SpringMvc+ajax 实现json格式数据传递
- spring-boot实现FastJson解析json数据
- 使用Struts2写的接口实现接收json格式的数据
- spring-boot 实现通过访问路径后加参数返回json数据
- spring boot 使用fastjson 处理json数据中文乱码 默认使用 ISO-8859-1编码格式