首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >akka-http复合json转换

akka-http复合json转换
EN

Stack Overflow用户
提问于 2017-07-13 03:17:35
回答 1查看 286关注 0票数 0

我想使用akka http实现restful http服务。[法]收货和做某事

代码语言:javascript
复制
{
    "user": {
        "userid": 123,
        "reqid": "abcdef",
        "username": "张三"
    },
    "article": {
        "atype": 1,
        "fromdate": "2017-06-27"
    },
    "tags": {
        "fixtags": [
            { "t":"t1", "w": 30 },
            { "t":"t2", "w": 20 },
            { "t":"t3", "w": 10 },
            { "t":"t4", "w": 50 }
        ],
        "atags": [
            { "t":"t5", "w": 30 },
            { "t":"t6", "w": 20 }
        ]
    }
}

案例类

代码语言:javascript
复制
case class Article(atype: Int, fromdate: String)
case class Tag(t: String, w: Double)
case class FixTags(fixtags: List[Tag])
case class ATags(atags: List[Tag])
//case class Tags( fixtags: List[Map[String, Float]], atags: List[Map[String, Float]] )
case class Tags(fixtags: FixTags, atags: ATags)

case class AUserInfo(user: User, article: Article, tags: Tags)
implicit val userFormat1 = jsonFormat3( User )
implicit val artFormat1 = jsonFormat2( Article )
implicit val tagFormat1 = jsonFormat2( Tag )
implicit val ftagFormat1 = jsonFormat1( FixTags )
implicit val atagFormat1 = jsonFormat1( ATags )
implicit val tagsFormat1 = jsonFormat2( Tags )

implicit val userFormat = jsonFormat3( AUserInfo )

路线:

代码语言:javascript
复制
path( "rcmd" / LongNumber ) {
            userid =>
                post {


                    entity( as[AUserInfo] ) { userinfo =>do sth}

asAUserInfo将报告错误:

请求内容格式错误:字段“fixtags”%中期望的对象

或者:

找不到参数um: akka.http.scaladsl.unmarshalling.Unmarshaller的隐式值

有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2017-07-13 03:46:47

我已经发现了错误:因为我的json字符串格式不适合我的case类。我将case类更改为下面,并修复该错误:

代码语言:javascript
复制
//case class FixTags(fixtags: List[Tag])
//case class ATags(atags: List[Tag])

case class Tags(fixtags: List[Tag], atags: List[Tag])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45070842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档