You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus中实现REST @PATCH方法遇400错误的问题排查

问题:Quarkus中RESTful PATCH方法返回400 Bad Request的排查与解决

问题背景

想要实现REST的@PATCH方法来更新实体部分字段,参考相关文档后仍一直收到400(Bad Request)响应。

复现步骤

  • 从quarkus.io加载Quarkus 2.15.3.Final初始项目,依赖为quarkus-resteasy-reactive-jackson
  • 添加User类:
package org.acme;

import java.util.Objects;

public class User {

  Integer id;
  String firstName;
  String lastName;

  public Integer getId() {return id;}
  public void setId(Integer id) {this.id = id;}
  public String getFirstName() {return firstName;}
  public void setFirstName(String firstName) {this.firstName = firstName;}
  public String getLastName() {return lastName;}
  public void setLastName(String lastName) {this.lastName = lastName;}

  @Override
  public String toString() {
    return String.format("User [ id: %d ]", Objects.requireNonNullElse(id, -1));
  }
}
  • 修改GreetingsResource.java:
package org.acme;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.reactive.RestPath;

@Path("/user")
public class GreetingResource {


    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{id}")
    public User getUser(Integer id) {
      User user = new User();
      user.id = 1;
      user.firstName = "John";
      user.lastName = "Doe";

      // return Response.noContent();
      return user;
    }

    @PATCH
    @Path("/{id}")
    @Consumes(MediaType.APPLICATION_JSON_PATCH_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public User patchUser(@RestPath("id") Integer id, User user) {
      return user;
    }
}
  • GET接口调用正常,请求示例:
GET http://localhost:8080/user/1 HTTP/1.1
Accept-Encoding: gzip,deflate
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.5 (Java/16.0.1)
  • PATCH请求返回400,请求示例:
PATCH http://localhost:8080/user/1 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json-patch+json
Content-Length: 53
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.5 (Java/16.0.1)

[{"op":"replace","path":"/lastName","value":"Smith"}]

响应:

HTTP/1.1 400 Bad Request
Content-Type: application/json
content-length: 0

错误日志

调整日志级别为debug后,捕获到以下错误:

2023-01-13 12:45:57,049 DEBUG [org.jbo.res.rea.ser.han.RequestDeserializeHandler] (executor-thread-0) Error occurred during deserialization of input: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `org.acme.User` from Array value (token `JsonToken.START_ARRAY`)
 at [Source: (org.jboss.resteasy.reactive.server.vertx.VertxInputStream); line: 1, column: 1]

疑问

应用当前期望接收User对象,而非发送的Patch请求数组。但根据文档理解,框架应自动使用Patch请求更新现有User实例,方法参数应返回已修补的User对象。请问哪里操作错误?是否对框架功能存在误解?

运行环境

  • Windows主机
  • Java版本:
$ java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

内容的提问来源于stack exchange,提问作者vizete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 23:40:22