Resource model validation failure in JerseyTest for Spring-Jersey application

丶灬走出姿态 提交于 2019-12-02 01:39:51
Paul Samsotha

Note: For the most part, this answer applies to all @XxxParam annotated parameters.

The PointDto parameter seems to be the problem

public UnitDto getUnits(@QueryParam("contains") @NotNull PointDto point)

Query parameters are Strings. Jersey has the capability to convert that String to some basic types like int, boolean, Date, etc. But it has no idea how to create the PointDto from that String. That doesn't mean that we can't use custom types. We just need to follow some rules:

  1. Have a constructor that accepts a String. Then we need to construct the object ourselves from that String. For example

    public PointDto(String param) {
        Map<String, Object> map = parseJson(param);
        this.field1 = map.get("field1");
        this.field2 = map.get("field2");
    }
    

    It's a bit of work.

  2. Another option is to have a static fromString(String) or static valueOf(String) in the class. Jersey will invoke this method to construct the object. So for instance you can have in your PointDto class

    public static PointDto fromString(String param) {
        PointDto dto = parseJson(param);
        return dto;
    }
    
  3. You can have a ParamConverterProvider. I won't put an example, but you can read more in this good article about how you can implement it. You can also see this post

Note, the information presented in this answer is in the javadoc for @QueryParam.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!