首页 > 解决方案 > 如何处理 REST 服务的输入枚举字段的验证错误?

问题描述

我有一个 REST 服务,它接受一个包含枚举字段的对象作为输入。当枚举中不存在的值在输入中传递给我时,会自动启动 BAD REQUEST。我试图进入调试以查看在哪里处理错误,但我注意到它并没有真正进入方法。

检索接口:

@POST
@Path("/retrieve")
public Response retrieveValue(@Valid User user);

用户:

public class User(){
  String name;
  String surname;
  CityEnum city;

  // getter and setter whith @NotNull annotation
}

城市枚举:

public enum CityEnum(){
   LONDON("London"),
   LIVERPOOL("Liverpool"),
   ...

  private String city;
}

我需要返回一条错误消息,并且我假设处理将在实现 Retrieve 接口的 RetrieveService 中完成。如何才能做到这一点?我正在使用微配置文件。

标签: javaapirestexceptionmicroprofile

解决方案


推荐阅读