Json 消费的 Jersey Post 请求 405 错误

Posted

技术标签:

【中文标题】Json 消费的 Jersey Post 请求 405 错误【英文标题】:Jersey Post Request 405 Error for Json Consumption 【发布时间】:2019-02-21 19:31:43 【问题描述】:

我正在尝试使用 Jersey 库构建一个 REST API。 API 中的 Post 方法应该使用 JSON 正文。当我尝试通过 Postman 发送请求时,我收到 405 错误。

@POST
@Path("CreateADGroup")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/json")
@RequiredRight(value = "createADGroup")
public Map postCreateADGroup(RequestValues requestValues) 
    log.debug("POST CreateADGroup");
    log.debug("This is the value" + requestValues.getValue());
    // using a test map untill i can get the actual values.
    Map ret = new HashMap();
    ret.put("Val", "TestVal");
    return ret;

这里是POJO方法

public class RequestValues 
String value;

public String getValue() 
    return value;


public void setValue(String value) 
    this.value = value;


我曾尝试使用 Direct JsonObject 初始化,但收到同样的错误。

@POST
@Path("CreateADGroup")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/json")
@RequiredRight(value = "createADGroup")
public Map postCreateADGroup(JSONObject requestValues) 
Map ret = new HashMap();
    ret.put("Val", "TestVal");
    return ret; 

我正在通过 POSTMAN 请求测试我的 API,我收到 405 Method Not Allowed。我已附上错误日志。

2019-02-21 11:17:40,137 ERROR http-nio-8080-exec-2 sailpoint.web.SailPointPollingRequestFilter:111 - javax.servlet.ServletException: javax.ws.rs.ProcessingException: Resource Java method invocation error.
javax.servlet.ServletException: javax.servlet.ServletException: javax.ws.rs.ProcessingException: Resource Java method invocation error.
    at sailpoint.web.SailPointContextRequestFilter.doFilter(SailPointContextRequestFilter.java:69)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sailpoint.web.SailPointPollingRequestFilter.doFilter(SailPointPollingRequestFilter.java:109)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sailpoint.web.ResponseHeaderFilter.doFilter(ResponseHeaderFilter.java:63)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.servlet.ServletException: javax.ws.rs.ProcessingException: Resource Java method invocation error.
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sailpoint.rest.jaxrs.MethodOverrideFilter.doFilter(MethodOverrideFilter.java:90)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sailpoint.rest.RestCsrfValidationFilter.doFilter(RestCsrfValidationFilter.java:59)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sailpoint.rest.AuthenticationFilter.doFilter(AuthenticationFilter.java:100)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at sailpoint.web.SailPointContextRequestFilter.doFilter(SailPointContextRequestFilter.java:52)
    ... 29 more
Caused by: javax.ws.rs.ProcessingException: Resource Java method invocation error.
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:147)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    ... 48 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    ... 64 more`

任何帮助表示赞赏。谢谢。

【问题讨论】:

【参考方案1】:

是的,我想出了答案。我将方法签名更改为 Map。

@POST
@Path("CreateADGroup")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/json")
@RequiredRight(value = "createADGroup")
public Map postCreateADGroup(Map requestValues) 
    log.debug("POST CreateADGroup");
    log.debug("This is the value" + requestValues.get("value"));
    Map ret = new HashMap();
    ret.put("Val", "No Val");
    return ret;

【讨论】:

以上是关于Json 消费的 Jersey Post 请求 405 错误的主要内容,如果未能解决你的问题,请参考以下文章

在API上的“post”方法获取“org.glassfish.jersey.message.internal.HeaderValueException”错误

POST 到 Jersey REST 服务收到错误 415 Unsupported Media Type

Jersey POST 请求并关闭 InputStream

有没有办法在 REST API Jersey 中检测预检请求?

jersey rest post 访问为http 405 异常如何解决

Jersey POST 方法正在接收空值作为参数