1

I'm new to scala and spray and I'm having a very simple problem. I want my rest service to return unescaped strings when I return a string from a rest call, it stead i'm getting a compacted string with escapes in it. Here is my code:

Rest Service: ....

get {
     respondWithMediaType(MediaTypes.`text/plain`) {
        complete {
                    s"""
hey joe this is 
"me" 
bill"""
          }
      }
   }

When I call the my service I get:

"\nhey joe this is\n\"me\"\n\bill"

But if I do a println() I see what I would expect. Please help.

4

2 回答 2

4

我解决了我自己的问题,它是自动编组对象的 JSON 支持特性

这个特性Json4sSupport自动将您的对象编组为 JSON,因此可以进行包装和紧凑打印等。

import spray.httpx.Json4sSupport

如果你删除它,你应该得到未转义的结果。

希望它可以帮助其他人。

于 2014-03-28T14:01:00.557 回答
3

我们在使用 Jackson 时遇到了同样的问题;我们通过创建一个 http 响应对象来解决这个问题,就像这样......</p>

              respondWithMediaType(`text/html`) {
                complete {
                  new HttpResponse(StatusCodes.OK, HttpEntity(
                    """
                    |<!DOCTYPE html>
                    |<html>
                    |<head>
                    |<title>Download app</title>
                    |</head>
                    |<body>
                    |<h2>
                    |Click <a href="/download/test.txt">here</a><br>
                    |</h2>
                    |</body>
                    |</html>
                  """.stripMargin
                  ))
                }
              }
于 2014-07-22T11:56:18.467 回答