我有一个像下面这样的课程。
public class MyDto {
private int id;
private String name;
private String address;
// getters and setters .....
}
我有一个像下面这样的 MS4J 服务,它从这个对象返回一个 JSON。
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.example.service.dto.MyDto;
import org.wso2.msf4j.Request;
@Path("/hello/1.[\\d]/version")
public class HelloService {
@GET
@Path("/{version}")
@Produces({"application/json"})
public Response hello(@PathParam("version") int version, @Context Request request) {
MyDto dtoObject = new MyDto(1, "TestObjName", "TestObjAddress");
if (version < 1) {
//return object without address
} else {
//return object with address
}
}
}
在运行时,我需要根据版本从 java 类中排除属性。IE。if version >1 object without address else object with the address property。
为地址字段设置 null 不是一种选择。我试图用 JacksonView 和 JacksonFilter 做到这一点,但无法适应这种情况。
任何想法或示例代码来解决这个问题?