Java jersey rest:如何返回接口而不是类
我有一个rest Web服务。由于某些原因,我不想返回需要转换为接口的类。我想返回接口,在JSON中我只想看到方法(开始get)值。不是实现类中的所有属性 举个例子:Java jersey rest:如何返回接口而不是类,java,json,rest,jersey,Java,Json,Rest,Jersey,我有一个rest Web服务。由于某些原因,我不想返回需要转换为接口的类。我想返回接口,在JSON中我只想看到方法(开始get)值。不是实现类中的所有属性 举个例子: @Path("getValues/") @GET @Produces( MediaType.APPLICATION_JSON) public DetailInterface getClientDetail() { return new DetailImpl(); } 并考虑接口: public interface
@Path("getValues/")
@GET
@Produces( MediaType.APPLICATION_JSON)
public DetailInterface getClientDetail() {
return new DetailImpl();
}
并考虑接口:
public interface DetailInterface {
public String getName();
public String getAge();
}
并考虑执行< /P>
public class DetailImpl implements DetailInterface {
public String getName()
return "my name";
}
public String getAge(){
return "my age";
}
public String iDontWantThisinJSON() {
return "I don't want this in JSON output";
}
}
当我请求rest服务时,我看到JSON响应中也有idontwanttthisingson
属性。我不想把它包括在回复中
如何修复此问题?您是否尝试过
@JsonIgnore
注释?将该字段标记为“瞬态”也可能有帮助-但是我不确定它是否正常。您是否尝试过@JsonIgnore
注释?将字段标记为transient
也可能有帮助,但是我不确定它是否正确。这是一个比Rest或Json更像java的概念。您必须按如下方式修改代码,并且它应该按照您希望的方式工作
@Path("getValues/")
@GET
@Produces( MediaType.APPLICATION_JSON)
public DetailInterface getClientDetail() {
DetailInterface di = new DetailImpl()
return di;
}
与Rest或Json相比,这更像是一个java概念。您必须按如下方式修改代码,并且它应该按照您希望的方式工作
@Path("getValues/")
@GET
@Produces( MediaType.APPLICATION_JSON)
public DetailInterface getClientDetail() {
DetailInterface di = new DetailImpl()
return di;
}
不,这不起作用。你在你的系统中验证了吗?不,这不起作用。你在你的系统中验证了吗?不,这不起作用。您在您的系统中验证了这一点?我们有任何其他选项?据我所知,没有。可能
瞬态
也会起作用。我们有任何其他选项?据我所知,没有。可能瞬态
也会起作用。我们有任何其他选项?据我所知,没有。可能瞬态
也会起作用。