【问题标题】:Does jetty overwrite custom http status code ?码头会覆盖自定义 http 状态码吗?
【发布时间】:2018-05-07 13:53:08
【问题描述】:

我正在尝试为我的服务开发一个 rest api,其中我根据授权失败设置自定义 http 状态代码。但是,当我使用 curl 对其进行测试时,我收到的是 404 而不是 403。我对可能导致这种情况的原因感到困惑?请帮忙。

这是我从 curl 输出或 swagger UI 中看到的:

root@ubuntu:~# curl -X GET http://localhost:8082/mr/v1/topic/bhakk -v
Note: Unnecessary use of -X or --request, GET is already inferred.
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8082 (#0)
> GET /mr/v1/topic/bhakk HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Mon, 07 May 2018 22:00:10 GMT
< Exception: serviceBlockedException
< Content-Type: application/vnd.kafka.v1+json
< Content-Length: 83
< Server: Jetty(9.2.z-SNAPSHOT)
<
* Connection #0 to host localhost left intact
{"error_code":40301,"message":"This service does not have access to the resource."}

代码如下:

    public Collection<String> list(@HeaderParam("x-nssvc-serviceid") String serviceID) {
        Date now = new java.util.Date();
        if (! ctx.getSecurityRestrictions().isServiceAllowed(uri, httpHeaders, "Describe", "Cluster", "kafka-cluster"))
          throw Errors.serviceBlockedException(ctx,httpServletResponse);

        List<String> topicsCopy = new ArrayList<String>(topics);
        for (Iterator<String> iterator = topicsCopy.iterator(); iterator.hasNext();) {
          String topic = iterator.next();
          if (! ctx.getSecurityRestrictions().hasAccess (serviceId, "Describe", "Topic", topic)) {
            iterator.remove();
          }
        }

        return topicsCopy;
      }

    public static RestException serviceBlockedException(Context ctx,HttpServletResponse httpServletResponse) {
        httpServletResponse.setHeader("Exception","serviceBlockedException");
        httpServletResponse.setStatus(Status.FORBIDDEN.getStatusCode()); <----// here i am setting status code.
        return new RestNotFoundException(SERVICE_ID_BLOCKED_MESSAGE, SERVICE_ID_BLOCKED_ERROR_CODE);
      }

【问题讨论】:

  • 好像你的 RestNotFoundException 正在其他地方将状态设置为 404。

标签: rest jetty jax-rs embedded-jetty


【解决方案1】:

【讨论】:

  • 当然。谢谢!
猜你喜欢
  • 2015-09-10
  • 1970-01-01
  • 2017-05-29
  • 2011-12-21
  • 1970-01-01
  • 2022-01-20
  • 2011-12-21
  • 2014-08-01
相关资源
最近更新 更多