2

私はjax rsを使用しており、jsonのコードとして応答を作成したい:

@Produces(MediaType.APPLICATION_JSON)
public class Rest{
@GET
Path("/Test")
public RestResponse restTest(){
return new RestResponse(100,"ERROR");
}
}

@XmlRootElement()
public class RestResponse{
private Integer code;
private String status;
private Integer id;

public RestResponse(Integer code, String status){
this.code = code;
this.status = status;
}
}

そして、私は次のような応答を得ています:
{"status":"ERROR","code":100,"id":null}

その値を削除するには"id":null?私は次のように見えるように応答したい: {"status":"ERROR","code":100}

idがnullの場合もあれば、コードがnullの場合もあり、表示したくない場合があるため、必要です。

4

1 に答える 1

2

Gensonを使用すると、次のように実行できます。

// register a provider for your custom genson configuration.
@Provider
public class GensonCustomResolver implements ContextResolver<Genson> {
  // configure the Genson instance
  private final Genson genson = new Genson.Builder()
                                        // if you want to support JAXB annotations
                                        .with(new JAXBBundle())
                                        .setSkipNull(true)
                                        .create();

  @Override
  public Genson getContext(Class<?> type) {
    return genson;
  }
}
于 2013-09-24T10:42:01.840 に答える