spring - 方法不允许 405,在 grails 从 2.5.2 升级到 3.3.11 后点击 POST 请求时

标签 spring grails grails-3.3.x

将 grails 2.5.2 升级到 3.3.11 后,我在点击 POST 请求时收到 Method Not Allowed 响应。 GET 方法工作正常。
Controller :

package omapi

import grails.rest.RestfulController
import org.springframework.http.HttpStatus

import static org.springframework.http.HttpStatus.NOT_FOUND
import static org.springframework.http.HttpStatus.NO_CONTENT

class PhaseController extends RestfulController {

PhaseController(){
    super(Phase)
}

static responseFormats = ['json', 'xml']
static allowedMethods = [show:  'GET', index:'GET',productAcronym:'GET', phaseByName: 'GET',save:'POST',update:'PUT',delete:'DELETE', deleteGroup: 'DELETE', deletePhase: 'DELETE']

def phaseService

def index(){
    def phases = phaseService.getAllPhases()
    respond phases
}

def show(){
    def phase = phaseService.getPhaseById(params.id)
    respond phase
}

def phaseByName(){
    def phase = phaseService.getPhaseByName(params?.name)
    respond phase
}

def productAcronym() {
    def phase = phaseService.getPhaseByProductAcronym(params?.acronym)
    log.info("==== phase by product Acronym =====$params.acronym==");
    respond phase;
}
  }
网址映射:
package omapi

import grails.core.GrailsApplication
import java.nio.file.AccessDeniedException

class UrlMappings {

    static mappings = {

        "/applications/$id/processingGroup"(controller: "application",action: "processingGroup")
        "/accounts/$id/applications/"(controller: "accounts", action: "applications")
        "/accounts/$aId/application/$id/products/"(controller: "application", action: "products")
        "/ftpServer/connection"(controller: 'ftpServer', action: 'testConnection')
        "/application/$id/jobs/"(controller: "job",action: "jobByApplication")
        "/application/cycleDates"(controller: "cycleDates",action: "getCycleDatesByApplication")
        "/ProcessingBranch/branch/"(controller: "processingTicketBranch",action: "branch")

        "/application/$appCode/Offset"(controller: "ReportClientOffset",action: "offsetValue")
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }
        "/$controller/$id"(parseRequest: true){
            action = [GET:"show",PUT:"update",POST:"404",DELETE:"delete"]
            constraints {
                id matches: /\d+/
            }
        }

        "/$controller"{
            action = [GET:"index",POST: "save",PUT:"update",DELETE:"delete"]
            constraints {
            }
        }

        "/"(view:"/index")
        "403"(controller: "error", action: "error403")
        "404"(controller: "error", action: "error404")
        "409"(controller: "error", action: "error409")
        "500"(controller: "error", action: "error500")
        "500"(controller: "error", action: "error403", exception: AccessDeniedException)
    }
}
请求:[POST] 本地主机:5555/OMApi/phase
回复:
{
    "timestamp": 1594295030496,
    "status": 405,
    "error": "Method Not Allowed",
    "message": "No message available",
    "path": "/OMApi/phase"
}
对于 grails 2.5.2,一切正常。它看起来像一个与 Spring 相关的问题。对此事的所有搜索都没有结果。任何的想法?是由于 UrlMapping 中的某些错误还是其他问题(例如 CORS Interceptor 无法正常工作)?

最佳答案

After upgrading grails 2.5.2 to 3.3.11, I am getting Method Not Allowed response while hitting POST request. The GET method works fine.


看起来你有 /OMApi/phase映射到 index行动在 PhaseController配置为 index: 'GET'allowedMethods这意味着 index操作只能通过 'GET' 访问要求。对于该操作,任何其他动词都应导致 405。如果您想同时允许 GETPOSTindex行动(不清楚你为什么要这样做)然后改变你的 allowedMethods包括 index: ['GET', 'POST']而不是 index: 'GET' .
我希望这有帮助。

关于spring - 方法不允许 405,在 grails 从 2.5.2 升级到 3.3.11 后点击 POST 请求时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62826870/

相关文章:

java - 如何在 Spring Security 登录中获取自定义用户实体以进行自定义身份验证?

Spring WebSocket超时设置

java - chalice 3 : Unit Testing Interceptors: Doesn't halt in interceptor

java - 如何在 hibernate 4 中定义获取 session 的事务?

javascript - 悬停时弹出,从 Mysql 数据库填写

spring - Grails:如何使用 bean 构建 DSL 指定 Spring 组件扫描排除

grails - 比较Grails GSP中的字符串

grails - 保存第二版本的grails对象

Grails 3.3.2 Spring Security CAS 不能通过负载均衡器工作,但可以通过部署在其上的内部服务器正常工作

grails - Grails 3.3.9:转发副本参数