SpringBoot使用教程【1】Restful API设计 返回json,xml格式

来源:互联网 发布:淘宝店铺 武汉飞鱼运动 编辑:程序博客网 时间:2024/05/21 13:45

效果展示:

浏览器截图
这里写图片描述

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 = " ")@RestControllerpublic 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或者String        return 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>() {                            @Override                            public 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>
原创粉丝点击