SpringBoot使用教程【1】Restful API设计 返回json,xml格式...
效果展示:
浏览器截图
http://localhost:8080/Chapter/getParam/app/xml
http://localhost:8080/Chapter/getParam/app/json
主要知识点:
- SpringBoot的使用
- HTTP Content-type
- Spring属性produces
- Restful API (根据不同参数返回不同响应格式)
- Okhttp的使用
服务器:
源码
package com.didispace.web;import java.io.IOException;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;import com.alibaba.fastjson.JSON;
import com.didispace.util.HttpRequestUtils;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;/*** 常见的媒体格式类型如下:text/html : HTML格式text/plain :纯文本格式 text/xml : XML格式image/gif :gif图片格式 image/jpeg :jpg图片格式 image/png:png图片格式以application开头的媒体格式类型:application/xhtml+xml :XHTML格式application/xml : XML数据格式application/atom+xml :Atom XML聚合格式 application/json : JSON数据格式application/pdf :pdf格式 application/msword : Word文档格式application/octet-stream : 二进制流数据(如常见的文件下载)application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)另外一种常见的媒体格式是上传文件之时使用的:multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式以上就是我们在日常的开发中,经常会用到的若干content-type的内容格式。* @author Arison**/
@Api(value = "get请求", description = " ")
@RestController
public class HttpGetController {@ApiOperation(value = "默认", notes = "")@RequestMapping(value = "/getParam", method = RequestMethod.GET)public @ResponseBody Map<String, Object> getParam(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return goods;}@ApiOperation(value = "text/html", notes = "text/html")@RequestMapping(value = "/getParam/html", method = RequestMethod.GET, produces = "text/html; charset=utf-8")public @ResponseBody Object getParamHtml(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);//注意返回类型需要是Object或者Stringreturn JSON.toJSONString(goods);}@ApiOperation(value = "text/plain", notes = "text/plain")@RequestMapping(value = "/getParam/text", method = RequestMethod.GET, produces = "text/plain; charset=utf-8")public @ResponseBody String getParamText(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return JSON.toJSONString(goods);}@ApiOperation(value = "text/xml", notes = "text/xml")@RequestMapping(value = "/getParam/xml", method = RequestMethod.GET, produces = "text/xml; charset=utf-8")public @ResponseBody Map<String, Object> getParamXml(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return goods;}@ApiOperation(value = "text/json", notes = "text/json")@RequestMapping(value = "/getParam/json", method = RequestMethod.GET, produces = "text/json; charset=utf-8")public @ResponseBody String getParamJson(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return JSON.toJSONString(goods);}@ApiOperation(value = "application/json", notes = "application/json")@RequestMapping(value = "/getParam/app/json", method = RequestMethod.GET, produces = "application/json; charset=utf-8")public @ResponseBody Map<String, Object> getParamAppJson(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return goods;}@ApiOperation(value = "application/xml", notes = "application/xml")@RequestMapping(value = "/getParam/app/xml", method = RequestMethod.GET, produces = "application/xml; charset=utf-8")public @ResponseBody Map<String, Object> getParamAppXml(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return goods;}@ApiOperation(value = "application/xhtml+xml", notes = "application/xhtml+xml")@RequestMapping(value = "/getParam/app/html", method = RequestMethod.GET, produces = "application/xhtml+xml ; charset=utf-8")public @ResponseBody Map<String, Object> getParamAppHtml(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return goods;}@ApiOperation(value = "application/text", notes = "application/text")@RequestMapping(value = "/getParam/app/text", method = RequestMethod.GET, produces = "application/text ; charset=utf-8")public @ResponseBody String getParamAppText(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {Map<String, Object> goods = HttpRequestUtils.getHttpMessage(request);return JSON.toJSONString(goods);}
}
客户端调用(Java或Android)
源码
public static final String BASE_URL = "http://192.168.253.200:8080/Chapter/";public static HttpClient httpClient = new HttpClient.Builder(BASE_URL)// 根路径.header("Cookie", "abdclejdldj82jk23jfjd")// 全局请求头 //局部可累加.header("Cache-control", "max-age=600").maxRetryCount(0)// 局部可覆盖.isDebug(false)// 局部可覆盖.retryTimeout(1000)// 局部可覆盖.cacheFile(new File("C:/Cache"))// 局部可覆盖.cacheFileSize(10240 * 1024)// 局部可覆盖.cacheType(CacheType.ONLY_NETWORK)// 局部可覆盖.cacheTime(60 * 200)// 设置10分钟 //局部可覆盖.connectTimeout(5000)// 局部可覆盖.readTimeout(5000)// 局部可覆盖.writeTimeout(7000)// 局部可覆盖.httpBase(RetrofitImpl.getInstance())// 局部可覆盖.build(true);// 保持单例httpClient.Api().send(new HttpClient.Builder().url("getParam")// 子路径.add("param3", "value1")// 局部参数.add("param4", "value2").header("cookies", "cookies")// 局部请求头.header("Accept","text/html,application/json,application/xml;q=0.9,image/webp,*/*;q=0.8").header("Cookie", "android")// 局部请求头.header("Cookie", "java")// 局部请求头---同名请求会覆盖.header("header3", "header1")// 局部请求头.header("header4", "header2")// 局部请求头.method(Method.GET).build(), new NetResquestSubscriber<Object>(new SubscriberOnNextListener<Object>() {@Overridepublic void onNext(Object t) {OkhttpUtils.println(t.toString());}}));
截图:
总结
- 本文重点在于控制响应头Content-Type的返回类型,来控制返回xml或者json格式。
- Springboot本身需要集成jackson-dataformat-xml来支持xml格式输出,否则会报406错误
<!-- xml输出 --><dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId></dependency>
SpringBoot使用教程【1】Restful API设计 返回json,xml格式...相关推荐
- Spring MVC同一方法返回JSON/XML格式
最近一道面试题,要求同一API接口支持不同格式返回值.一开始是设想通过过滤器(Filter)设置返回值,但是并不可行,因为方法返回值一般都是类型需要做转换,而过滤器则是前置的.另一方面可以通过拦截器的 ...
- 玩转 SpringBoot 2 快速整合 | RESTful Api 篇
概述 RESTful 是一种架构风格,任何符合 RESTful 风格的架构,我们都可以称之为 RESTful 架构.我们常说的 RESTful Api 是符合 RESTful 原则和约束的 HTTP ...
- 我是如何根据豆瓣api来理解Restful API设计的
1.什么是REST REST全称是Representational State Transfer,表述状态转移的意思.它是在Roy Fielding博士论文首次提出.REST本身没有创造新的技术.组件 ...
- RESTful架构和RESTful API设计总结
作者:智明书 链接:https://www.jianshu.com/p/955eb2faa354 REST这个词是2000年Roy Fielding在他的博士论文中提出的,Fielding参与了htt ...
- [译] RESTful API 设计最佳实践
https://juejin.im/entry/6844903503953920007 [译] RESTful API 设计最佳实践 阅读 8779 收藏 0 2017-10-16 原文链接: seg ...
- Restful API 设计
1. 简介 目前 "互联网软件"从用客户端/服务端模式,建立在分布式体系上,通过互联网通讯,具有高延时.高开发等特点.但是软件开发和网络是两个不同的领域,交集很少.要使得两个融合, ...
- RESTful API 设计指南 (转)
RESTful API 设计指南 2016-02-23 ImportNew (点击上方公号,可快速关注) 作者:阮一峰 链接:http://www.ruanyifeng.com/blog/2014/0 ...
- RESTful API 设计思考
RESTful API 设计思考,内容来源网络加自己的思考 1.RESTful Web API采用面向资源的架构:同一的接口,所以其成员体现为针对同一资源的操作 2.SOAP Web API采用RPC ...
- (附源码)Springboot美食教程小程序的设计与实现 毕业设计190900
Springboot美食教程小程序的设计与实现 摘 要 本文设计了一种基于微信小程序的美食教程小程序,系统为人们提供了方便快捷.即用即搜的美食教程服务,包括美食资讯.美食话题.注册登录等,用户能够方便 ...
最新文章
- UIPickerView
- 专访 | 德国大神Hans Uszkoreit:语言才是AI的关键,深度学习无法解决NLP的核心问题...
- PhoneGap/Cordova Android应用签名发布系列问题处理收集
- linux程序设计百度网盘,linux系统编程视频 百度网盘下载
- android AlertDialog.Builder
- HTMLCSS 超简单的前端设计入门-2!
- 【渝粤教育】 国家开放大学2020年春季 1001中国法制史 参考试题
- java反射 pdf_java反射学习笔记整理.pdf
- 【HDU - 5500】 Reorder the Books (思维)
- 腾讯视频与中国电信签署战略合作协议 共同成立5G联合实验室
- 打表(1215)七夕节
- 求出字符“2013-9-8”与“2013-10-1”相差的天数
- 电脑运行很慢怎么办_为什么电脑用久了,就算重新安装系统也会变得很慢?
- c语言程序由哪三个部分组成部分,C语言程序的组成部分
- 三菱plc pwm指令_【三菱PLC指令教程】定时器指令应用之时序图编程法(一)
- c语言中judge的用法,judge的用法
- EXCEL( 获得指定列的最后一个非空单元格)
- turtle库画一个正方形
- intel无线网卡的型号和驱动一览
- 前女友让我撸个植物大战僵尸,我一怒之下把代码开源了...
热门文章
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
- cocos2d ccLayer响应触摸事件方法:CCStandardTouchDelegate 与 CCTargetedTouchDelegate
- c#调用cmd执行相关命令
- scrapyd部署爬虫项目到LINUX服务器
- JQuery Tables 的应用(一)
- mycat教程(一) —— 安装
- Linux 小知识翻译 - 「架构」(arch)
- 每天5道面试题(二)java基础
- IoT机会窗打开:运营商亟需战略转型 获取物联网最大化价值
- 在Hive中执行DDL之类的SQL语句时遇到的一个问题