【问题标题】:Restlet Getting HTTP Status code 204 instead of 200Restlet 获取 HTTP 状态码 204 而不是 200
【发布时间】:2016-10-24 20:27:25
【问题描述】:

对于第一个请求,我得到 JSON 响应。从下一个请求开始,我开始获取此日志和 HTTP 状态代码 204,即使 ServerResource 已成功返回表示

org.restlet.engine.adapter.ServerAdapter commit
WARNING: A response with an unavailable and potentially non empty entity was returned. Ignoring the entity for resource http://localhost:8888/xyz?abc=def

布线应用类

@Override
public Restlet createInboundRoot() {
    router = new Router(getContext());
    CorsService corsService = new CorsService();         
    corsService.setAllowedOrigins( new HashSet<String>(Arrays.asList("http://example.com")));
    corsService.setAllowedCredentials(true);
    getServices().add(corsService);
    router.attach("/xyz", XYZ.class);
}

处理并返回 JSON 表示的服务器资源

public class XYZ extends ServerResource {

    private static final Logger logger = Logger.getLogger("API:XyZ");

    @Get(":json")
    public Representation handleGetRequest() {
         ..
         return API_RESPONSE_JSON_REPRESENTATION_SUCCESS;
    }
}

【问题讨论】:

    标签: java google-app-engine restlet


    【解决方案1】:

    释放响应后,表示状态available设置为false。因此,对ServerResource 的后续调用会返回表示,但在handle() 方法中它设置为204,因为getResponseEntity().isAvailable() 返回false

    来自ServerResource

    @Override
    public Representation handle() {
           ...
            } finally {
                if (Status.CLIENT_ERROR_METHOD_NOT_ALLOWED.equals(getStatus())) {
                    updateAllowedMethods();
                } else if (Status.SUCCESS_OK.equals(getStatus())
                        && (getResponseEntity() == null || !getResponseEntity()
                                .isAvailable())) {
                    getLogger()
                            .fine("A response with a 200 (Ok) status should have an entity. "
                                    + "Changing the status to 204 (No content).");
                    setStatus(Status.SUCCESS_NO_CONTENT);
                }
            }
        }
        return result;
    }
    

    解决方案

    要么每次都返回一个新的表示,要么在返回表示之前将 setAvailable 设置为 true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2012-03-09
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多