来自 Jersey 服务器端点的错误请求

Bad Request from Jersey server endpoint

我一直在查看提交请求时发生的问题;

X-Forwarded-For: so.me.ip.ad
Authorization: Bearer blahblahblahstuff
Accept: application/json
Content-Type: application/json; charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.8.0_60
Host: the.endpoint.io
Connection: keep-alive
Content-Length: 395 

具有语法正确的 Gson json 正文。它的长度完全相同,而且形状很好。我使用 wireshark 来捕获准确的响应并将数字相加。即,我认为请求主体的末尾没有膨胀。请告诉我 wireshark 是否无法接收到它。考虑到这一点,我认为修复 here 不适用于此问题。

我一直在本地测试运行 payara,生产服务器也是payara(完全相同的版本)。在本地,请求很好,按预期工作,使用完全相同的实时参数,没有到达端点,因为我们处理我们自己的 api 错误代码,这个在 html 中给出,不是json,下面可以看到;

Payara Server 4.1.2.173 #badassfish - Error report

HTTP Status 400 - Bad Request


type: Status report

message: Bad Request

description: The request sent by the client was syntactically incorrect.


Payara Server 4.1.2.173 #badassfish

我们正在使用 HttpUrlConnection 库执行 post;

        URL url = new URL(uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if (this.clientip != null && !this.clientip.isEmpty())
        {
            connection.addRequestProperty("X-Forwarded-For", this.clientip);
        }
        if (this.vauthtoken != null && !this.vauthtoken.isEmpty())
        {
            connection.addRequestProperty("Authorization", "Bearer " + this.vauthtoken);
        }
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setRequestMethod(this.method);
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        if(this.body != null)
        {
            try (OutputStream os = connection.getOutputStream()) {
                os.write(this.body.toString().getBytes("UTF-8"));
            }
        }

        if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            response = this.getJsonBody(connection.getInputStream());
        }
        else
        {
            response = this.getJsonBody(connection.getErrorStream());
        }

我们的端点看起来像;

@Path("/some/path")
public class SomeClass extends SomeThingelse {

    final static Logger LOGGER = Logger.getLogger(SomeClass.class.getName());

    @POST
    @Audit
    @Secured(roles = {Role.SOMEROLE}, crud = Crud.CREATE)
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response someMethodRelating(@Context SecurityContext securityContext, @Valid SomeValidatedClass svClass)
    {

我错过了什么吗?或者这更多是服务器问题?如有任何帮助,我们将不胜感激。

您没有使用正确的方法(使用 GET 而不是 POST),或者您发送的正文不是 JSON 或者没有验证 [=10] 中的规则=].