duancaozen6066 2017-06-13 08:05
浏览 58
已采纳

如何使用Google / jsonapi和echo框架返回有效的jsonapi响应

The code bellow return a two concated JSON strings and a wrong content-type text/plain. Should be application/vnd.api+json

package main

import (
    "github.com/google/jsonapi"
    "github.com/labstack/echo"
    "net/http"
)

type Album struct {
    ID   int    `jsonapi:"primary,albums"`
    Name string `jsonapi:"attr,name"`
}

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        jsonapi.MarshalManyPayload(c.Response(), albumList())
        return c.JSON(http.StatusOK, c.Response())
    })
    e.Logger.Fatal(e.Start(":1323"))
}

func albumList() []*Album {
    a1 := Album{123, "allbum1"}
    a2 := Album{456, "allbum2"}
    albums := []*Album{&a1, &a2}
    return albums
}

faulty output (two concated jsons). The first is a correct jsonapi structure and I think the second is related to echo-framework:

{
  "data": [
    {
      "type": "albums",
      "id": "123",
      "attributes": {
    "name": "allbum1"
      }
    },
    {
      "type": "albums",
      "id": "456",
      "attributes": {
    "name": "allbum2"
      }
    }
  ]
}
{
  "Writer": {},
  "Status": 200,
  "Size": 133,
  "Committed": true
}

This code fix the problem but is seems awkward. I have the feeling there is a better way to facilitate it using echo.

e.GET("/", func(c echo.Context) error {
    var b bytes.Buffer
    body := bufio.NewWriter(&b)
    err := jsonapi.MarshalManyPayload(body, albumList())
    if err != nil {
        fmt.Println(err)
    }
    body.Flush()
    return c.JSONBlob(http.StatusOK, b.Bytes())
})

Any idea?

  • 写回答

1条回答 默认 最新

  • duanji1899 2017-06-13 17:47
    关注

    You're code looks alright. However it can be simplified-

    var b bytes.Buffer // you could use buffer pool here
    err := jsonapi.MarshalManyPayload(&b, albumList())
    if err != nil {
        return err
    }
    return c.JSONBlob(http.StatusOK, b.Bytes())
    

    Following approaches for your thoughts:

    Approach 1 -

    c.Response().Header().Set(echo.HeaderContentType, jsonapi.MediaType)
    c.Response().WriteHeader(http.StatusOK)
    return jsonapi.MarshalManyPayload(c.Response(), albumList())
    

    Approach 2 -

    var b bytes.Buffer // you could use buffer pool here
    err := jsonapi.MarshalManyPayload(&b, albumList())
    if err != nil {
        return err
    }
    c.Response().Header().Set(echo.HeaderContentType, jsonapi.MediaType)
    c.Response().WriteHeader(http.StatusOK)
    _, err := b.WriteTo(c.Response())
    return err
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配