【问题标题】:Grails 3 rest profile 422 Unprocessable Entity result on show actionGrails 3 rest profile 422 Unprocessable Entity 结果显示动作
【发布时间】:2016-12-26 21:25:48
【问题描述】:

我遇到了麻烦,我无法处理它,我在这里没有找到任何答案。

我在 Grails 3.1.10 中有一个应用程序,带有其余配置文件,我有一些域类及其各自的控制器,我可以使用 POST 创建新对象以保存操作,使用 PUT 更新它们以更新操作并使用 GET 列出它们索引操作,但我无法在 GET 中获得正确的响应来显示操作。

我不确定我遇到的问题是在请求中还是在应用程序中

我的 UrlMappings 是这样的:

"/$controller/$action?/$id?(.$format)?"{
       constraints {
           // apply constraints here
       }

我也试过

get "/resources/$id"(controller:"resource", action:"show")

同样的结果:422: Unprocessable Entity, "errors": [10] 0: { "message": "类 [class ] 的属性 [] 不能为 null" "path": "/1" "_links": { "self": { "href": "http://localhost:8080/1" }- }-} 对于每个与 ​​id 不同的属性。

我正在向http://localhost:8080/resource/show/1(或第二个配置中的http://localhost:8080/resources/1)发出http get请求,标题如下:“Content-Type: application/json”和“Authorization: Bearer”(我使用的是spring安全休息插件来保护应用程序)。对http://localhost:8080/resource/index(或第二个配置中的http://localhost:8080/resources/)的相同请求运行完美,并返回了一个包含所有对象的json。

干杯!

【问题讨论】:

    标签: hibernate rest http grails http-headers


    【解决方案1】:

    我认为您应该检查以下两个选项之一:

    1. 在你的域类中使用@Resource,不要创建控制器(在这种情况下grails会自动生成它)

    2. 如果您想获得更多控制权,可以使用 url 映射资源 + 扩展 RestfulController 的控制器,如文档部分 10.1.5.1 (http://docs.grails.org/latest/guide/single.html#webServices) 中所述

    在第二个选项中,您将需要在 URlMappings 中这样的内容:

    "/books"(resources:"book")
    

    还有一个控制器,例如:

    class BookController extends RestfulController {
      static responseFormats = ['json', 'xml']
      BookController() {
        super(Book)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2022-09-30
      • 1970-01-01
      • 2022-08-14
      相关资源
      最近更新 更多