简体   繁体   English

Restlet获取HTTP状态代码204而不是200

[英]Restlet Getting HTTP Status code 204 instead of 200

For the 1st request, I get the JSON response. 对于第一个请求,我得到了JSON响应。 From the next request onwards I start getting this log and HTTP Status Code 204, even though the ServerResource is successfully returning a representation 从下一个请求开始,即使ServerResource成功返回了表示形式,我也开始获取此日志和HTTP状态代码204

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

Application class for wiring routes 布线路线的应用类别

@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);
}

Server Resource which handles and returns a JSON Representation 服务器资源,用于处理并返回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;
    }
}

After releasing the response, the representation state available is set to false. 释放响应后, available的表示状态将设置为false。 So subsequent calls to the ServerResource , returns the Representation but in handle() method it is set to 204 since getResponseEntity().isAvailable() returns false . 因此,随后对ServerResource调用返回了Representation,但在handle()方法中将其设置为204,因为getResponseEntity().isAvailable()返回false

From ServerResource : 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;
}

SOLUTION

Either return a new representation every time or setAvailable to true before returning the representation 每次返回一个新的表示形式,或者在返回该表示形式之前将setAvailable设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回200而不是204的响应代码 - Returning 200 response code instead of 204 即使将204作为HTTP状态代码,实体也不会保存在数据库中 - Entity not getting saved in database even though getting 204 as HTTP Status code HTTP 500错误失败,并显示200状态代码 - HTTP 500 error failed with 200 status code 为什么我的服务器响应状态代码是200而不是200 OK? - Why my server response status code is 200 instead of 200 ok? Jersey&#39;NoContent&#39;响应返回200而不是204 - Jersey 'NoContent' response returns 200 instead of 204 我正在获取 200 状态码,但我应该获取 302 状态码,如何解决? - I Am Getting The 200 Status Code But Instead I Should get 302 Status Code, How Do I Solve It? Jetty 在使用 http2 时响应状态 200 而不是 304 - Jetty respond with status 200 instead of 304 while using http2 泽西岛:返回204状态而不是500 - Jersey: Returns 204 Status Instead of 500 如何在RESTful Web服务中使用jersey框架抛出HTTP 204状态代码? - how to throw HTTP 204 status code using jersey framework in RESTful web service? 使用java连接到站点和POST,HTTP状态代码200 - Using java to connect to a site and POST, HTTP Status Code 200
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM