Lift REST 服务无法识别 PUT 请求

发布于 2024-12-03 01:08:08 字数 2205 浏览 0 评论 0原文

我正在 Scala 中使用 Lift Web 框架实现 REST 服务,但我在 PUT 请求方面遇到了一些问题创建一个我知道其 ID 的新实体。

已经将调度添加到 Boot.scala 中,我的其余服务对象看起来有点像这样:

package code
package lib

import model._

import net.liftweb._
import common._
import http._
import rest._
import util._
import Helpers._
import json._

object RestService extends RestHelper {

  serve( "api" / "room" prefix {
    // /api/room returns all the rooms
    case Nil JsonGet _ => Room.registredRooms: JValue
    // /api/room/count gets the room count
    case "count" :: Nil JsonGet _ => JInt(Room.registredRooms.length)
    // /api/room/room_id gets the specified room (or a 404)
    case Room(room) :: Nil JsonGet _ => room: JValue
    // DELETE the room in question
    case Room(room) :: Nil JsonDelete _ => 
      Room.delete(room.id).map(a => a: JValue)
    // PUT adds the room if the JSON is parsable
    case Nil JsonPut Room(room) -> _ => Room.add(room): JValue

    // POST if we find the room, merge the fields from the 
    // the POST body and update the room
    case Room(room) :: Nil JsonPost json -> _ => 
      Room(mergeJson(room, json)).map(Room.add(_): JValue)
  })

}

GET 请求在我测试时正常工作:

$ curl http://localhost:8080/api/room/abs
{
  "id":"abs"
}

我现在正在尝试实现创建服务,并且不断收到PUT 时未找到 404:

$ curl -i -H "Accept: application/json" -X PUT -d "[{'id':'abs'}]" http://localhost:8080/api/room/
HTTP/1.1 404 Not Found
Expires: Sun, 4 Sep 2011 14:13:50 UTC
Set-Cookie: JSESSIONID=t1miz05pd5k9;Path=/
Content-Length: 106
Cache-Control: no-cache, private, no-store
Content-Type: text/html; charset=utf-8
Pragma: no-cache
Date: Sun, 4 Sep 2011 14:13:50 UTC
X-Lift-Version: 2.4-M3
Server: Jetty(6.1.22)

<!DOCTYPE html>
<html> <body>The Requested URL /api/room/ was not found on this server</body> </html>

在 SBT 上,我可以看到该请求被识别为 PUT 请求:

15:13:50.130 [414557038@qtp-135607724-2 - /api/room/] INFO  net.liftweb.util.TimeHelpers - Service request (PUT) /api/room/ returned 404, took 10 Milliseconds

对可能出现的问题有什么想法吗?

I'm implementing a REST service with the Lift web framework in Scala and I'm having some issues with the PUT request to create a new entity for which I know the ID.

Already added the dispatch to Boot.scala and my rest service object looks a bit like this:

package code
package lib

import model._

import net.liftweb._
import common._
import http._
import rest._
import util._
import Helpers._
import json._

object RestService extends RestHelper {

  serve( "api" / "room" prefix {
    // /api/room returns all the rooms
    case Nil JsonGet _ => Room.registredRooms: JValue
    // /api/room/count gets the room count
    case "count" :: Nil JsonGet _ => JInt(Room.registredRooms.length)
    // /api/room/room_id gets the specified room (or a 404)
    case Room(room) :: Nil JsonGet _ => room: JValue
    // DELETE the room in question
    case Room(room) :: Nil JsonDelete _ => 
      Room.delete(room.id).map(a => a: JValue)
    // PUT adds the room if the JSON is parsable
    case Nil JsonPut Room(room) -> _ => Room.add(room): JValue

    // POST if we find the room, merge the fields from the 
    // the POST body and update the room
    case Room(room) :: Nil JsonPost json -> _ => 
      Room(mergeJson(room, json)).map(Room.add(_): JValue)
  })

}

GET requests are working properly as I tested with:

$ curl http://localhost:8080/api/room/abs
{
  "id":"abs"
}

I am now trying to implement the create service and I keep getting a 404 not found when I PUT:

$ curl -i -H "Accept: application/json" -X PUT -d "[{'id':'abs'}]" http://localhost:8080/api/room/
HTTP/1.1 404 Not Found
Expires: Sun, 4 Sep 2011 14:13:50 UTC
Set-Cookie: JSESSIONID=t1miz05pd5k9;Path=/
Content-Length: 106
Cache-Control: no-cache, private, no-store
Content-Type: text/html; charset=utf-8
Pragma: no-cache
Date: Sun, 4 Sep 2011 14:13:50 UTC
X-Lift-Version: 2.4-M3
Server: Jetty(6.1.22)

<!DOCTYPE html>
<html> <body>The Requested URL /api/room/ was not found on this server</body> </html>

On SBT I can see that the request is being recognized as a PUT request:

15:13:50.130 [414557038@qtp-135607724-2 - /api/room/] INFO  net.liftweb.util.TimeHelpers - Service request (PUT) /api/room/ returned 404, took 10 Milliseconds

Any ideas on what might be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2024-12-10 01:08:08

您测试 PUT 请求的方式存在三个问题。

最重要的是,您需要将 Content-Type 标头设置为 application/json (代替或补充 Accept 标头)。

接下来,您需要在 JSON 中使用双引号:-d '[{"id":"abs"}]'。 (实际上,有效 JSON 中的字符串需要双引号。一些 JSON 解析器会接受单引号,但 Lift 不会。)

最后,从 URL 中删除尾部斜杠。它将 "index" 添加到路径列表的末尾,这意味着您不会在 case Nil JsonPut... 行中获得匹配项。

以下应该有效:

curl -i -H "Content-Type: application/json" -X PUT -d '[{"id":"abs"}]' http://localhost:8080/api/room

There are three problems with the way you're testing the PUT request.

Most importantly, you need to set the Content-Type header to application/json (instead of or in addition to the Accept header).

Next, you need to use double quotation marks in your JSON: -d '[{"id":"abs"}]'. (Double quotation marks are actually required for strings in valid JSON. Some JSON parsers will accept single quotation marks, but not Lift's.)

Finally, remove the trailing slash from the URL. It's adding "index" to the end of the path list, which means you won't get a match in your case Nil JsonPut... line.

The following should work:

curl -i -H "Content-Type: application/json" -X PUT -d '[{"id":"abs"}]' http://localhost:8080/api/room
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文