Spring 未找到使用域类资源创建的Grails REST API返回
我正在尝试创建一个用于用户编辑的RESTAPI 我决定使用文档中描述的方法: (我使用的是较旧的GrailVersion=3.3.11) 因此,我继续创建了这样的域类(域类是由命令grailss2quickstart com.mastiko.auth User Role创建的) 此外,我将此配置添加到application.yml中,因为没有它,我无法使SpringSecurityRESTAPI插件工作Spring 未找到使用域类资源创建的Grails REST API返回,spring,rest,grails,groovy,Spring,Rest,Grails,Groovy,我正在尝试创建一个用于用户编辑的RESTAPI 我决定使用文档中描述的方法: (我使用的是较旧的GrailVersion=3.3.11) 因此,我继续创建了这样的域类(域类是由命令grailss2quickstart com.mastiko.auth User Role创建的) 此外,我将此配置添加到application.yml中,因为没有它,我无法使SpringSecurityRESTAPI插件工作 plugin: springsecurity: controllerAnnota
plugin:
springsecurity:
controllerAnnotations:
chainMap:
'/api/**': 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter'
'/**': 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'
My UrlMappings.groovy:
package mems****eprojects (censored)
class UrlMappings {
static mappings = {
delete "/$controller/$id(.$format)?"(action:"delete")
get "/$controller(.$format)?"(action:"index")
get "/$controller/$id(.$format)?"(action:"show")
post "/$controller(.$format)?"(action:"save")
put "/$controller/$id(.$format)?"(action:"update")
patch "/$controller/$id(.$format)?"(action:"patch")
"/"(controller: 'application', action:'index')
}
}
现在,当我尝试使用POST请求访问api时http://localhost:8080/api/users,我总是收到404未找到响应:
{
"timestamp": 1598893213831,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/users"
}
我尝试了一个命令grails url mappings report,得到了一个积极的结果:
Dynamic Mappings
| GET | /${controller}(.${format)? | Action: index |
| POST | /${controller}(.${format)? | Action: save |
| DELETE | /${controller}/${id}(.${format)? | Action: delete |
| GET | /${controller}/${id}(.${format)? | Action: show |
| PUT | /${controller}/${id}(.${format)? | Action: update |
| PATCH | /${controller}/${id}(.${format)? | Action: patch |
Controller: application
| * | / | Action: index |
Controller: restOauth
| * | /oauth/access_token | Action: accessToken |
| * | /oauth/${action}/${provider} | Action: (default action) |
Controller: user
| GET | /api/users/create | Action: create |
| GET | /api/users/${id}/edit | Action: edit |
| POST | /api/users | Action: save |
| GET | /api/users | Action: index |
| DELETE | /api/users/${id} | Action: delete |
| PATCH | /api/users/${id} | Action: patch |
| PUT | /api/users/${id} | Action: update |
| GET | /api/users/${id} | Action: show |
我是grails的新手,这是我的第一个应用程序
我很抱歉,如果一些重要的部分代码丢失,我会张贴它,如果需要的话。你能帮帮我吗?我花了好几个小时想弄清楚这件事
//编辑1
我忘了添加我的build.gradle
buildscript {
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "com.moowork.gradle:gradle-node-plugin:1.2.0"
classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}"
classpath "org.grails.plugins:views-gradle:1.2.9"
}
}
version "0.1"
group "mem****eprojects"
apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"com.moowork.node"
apply plugin:"org.grails.plugins.views-json"
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-plugin-url-mappings"
compile "org.grails:grails-plugin-rest"
compile "org.grails:grails-plugin-codecs"
compile "org.grails:grails-plugin-interceptors"
compile "org.grails:grails-plugin-services"
compile "org.grails:grails-plugin-datasource"
compile "org.grails:grails-plugin-databinding"
compile "org.grails:grails-web-boot"
compile "org.grails:grails-logging"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:async"
compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.1.16.Final"
compile "org.grails.plugins:views-json"
compile "org.grails.plugins:views-json-templates"
compile "org.grails.plugins:spring-security-core:3.2.0"
compile "org.grails.plugins:spring-security-rest:2.0.0.M2"
console "org.grails:grails-console"
profile "org.grails.profiles:vue"
runtime "org.glassfish.web:el-impl:2.1.2-b03"
runtime "com.h2database:h2"
runtime "org.apache.tomcat:tomcat-jdbc"
testCompile "org.grails:grails-gorm-testing-support"
testCompile "org.grails:grails-datastore-rest-client"
testCompile "org.grails:grails-web-testing-support"
}
bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
addResources = true
String springProfilesActive = 'spring.profiles.active'
systemProperty springProfilesActive, System.getProperty(springProfilesActive)
}
我添加了SpringSecurityREST插件,以便能够使用/api/loginEndpoint从vuejs前端对自己进行身份验证。我检索令牌,然后尝试在标头中使用令牌向/api/users端点发出请求。所以这个问题可能是由这个插件引起的,所以我能够修复下面从application.yml中删除代码的问题
plugin:
springsecurity:
controllerAnnotations:
chainMap:
'/api/**': 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter'
'/**': 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'
并将下面的代码添加到application.groovy
grails.plugin.springsecurity.filterChain.chainMap = [
//Stateless chain
[ pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter'],
//Traditional chain
//[ pattern: '/**', filters: 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter']
]
我真的不知道为什么我把chainMap放在application.yml的第一位
感谢您阅读本文并尝试帮助我,因此我能够修复下面从application.yml中删除的代码
plugin:
springsecurity:
controllerAnnotations:
chainMap:
'/api/**': 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter'
'/**': 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'
并将下面的代码添加到application.groovy
grails.plugin.springsecurity.filterChain.chainMap = [
//Stateless chain
[ pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter'],
//Traditional chain
//[ pattern: '/**', filters: 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter']
]
我真的不知道为什么我把chainMap放在application.yml的第一位
感谢您阅读本文并尝试帮助我我已将您的代码粘贴到项目中,无法重现您看到的行为。请访问。@JeffScottBrown您好,非常感谢您的尝试。我看到您有一个不同的UrlMappings(),我试图使用您的UrlMappings,现在我得到一个服务器错误。javax.servlet.ServletException:无法解析名为“grailsDispatcherServlet”的servlet中名为“denied”的视图“我看到您有不同的URL映射”-提交时使用的URL映射与上面显示的相同。这不应该导致404,我不能复制404。这些映射不会直接与标记为
User
的@Resource
类相关,因为您提供了uri
作为注释属性。当一个未经身份验证的请求被发送到/api/users
时,应该发生的是一个302,这就是我所看到的。在名为“GrailDispatcherservlet”的servlet中,“无法解析名为“denied”的视图”是一个与问题中询问的问题不同的问题。确定。谢谢你的解释。我忘了在这里提到的是,我正试图让它与spring security rest插件compile“org.grails.plugins:spring security core:3.2.0”compile“org.grails.plugins:spring security rest:2.0.0.M2”一起工作,我调用/api/login,我使用在bootstrap中创建的用户登录,我检索一个令牌,然后使用该令牌访问/api/users端点。因此,我想知道是否有可能安全rest插件以某种方式破坏了meI的行为,因为meI已将您的代码粘贴到项目中,并且无法重现您看到的行为。请访问。@JeffScottBrown您好,非常感谢您的尝试。我看到您有一个不同的UrlMappings(),我试图使用您的UrlMappings,现在我得到一个服务器错误。javax.servlet.ServletException:无法解析名为“grailsDispatcherServlet”的servlet中名为“denied”的视图“我看到您有不同的URL映射”-提交时使用的URL映射与上面显示的相同。这不应该导致404,我不能复制404。这些映射不会直接与标记为User
的@Resource
类相关,因为您提供了uri
作为注释属性。当一个未经身份验证的请求被发送到/api/users
时,应该发生的是一个302,这就是我所看到的。在名为“GrailDispatcherservlet”的servlet中,“无法解析名为“denied”的视图”是一个与问题中询问的问题不同的问题。确定。谢谢你的解释。我忘了在这里提到的是,我正试图让它与spring security rest插件compile“org.grails.plugins:spring security core:3.2.0”compile“org.grails.plugins:spring security rest:2.0.0.M2”一起工作,我调用/api/login,我使用在bootstrap中创建的用户登录,我检索一个令牌,然后使用该令牌访问/api/users端点。因此,我想知道是否有可能安全rest插件以某种方式破坏了我的行为