Apache Syncope modify @XmlElement

130 Views Asked by At

I have a Java Backend responding rest request with response with this class:

import java.util.Collection;
import java.util.Collections;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.map.annotate.JsonSerialize;

@XmlRootElement
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class RestResponse<T> {
  @XmlElement(name = "METADATA")
  private JsonMetadata  jsonMetadata;
  private Collection<T> result;

  public RestResponse() {
    jsonMetadata = new JsonMetadata();
  }

  public RestResponse(JsonMetadata metadata) {
    this.jsonMetadata = metadata;
  }

  public JsonMetadata getJsonMetadata() {
    return jsonMetadata;
  }

  public void setJsonMetadata(JsonMetadata jsonMetadata) {
    this.jsonMetadata = jsonMetadata;
  }

  public Collection<T> getResult() {
    return result;
  }

  public void setResult(Collection<T> result) {
    this.result = result;
  }

  public void setObjectList(Collection<T> objectList) {
    if (objectList != null) {
      this.result = objectList;  
    }    
  }

  public void setObject(T object) {
    if (object != null) {
      setObjectList(Collections.singletonList(object));
    }
  }

  public void setErrorMessage(String msg) {
    jsonMetadata.setErrorMessage(msg);
  }

  public void setWarnMessage(String msg) {
    jsonMetadata.setWarnMessage(msg);
  }

}

And works ok sending something like this:

METADATA: {STATUS: "0", ERROR_MESSAGE: ""} 
result: [{id: "4010", name: "Demo"}]

Now I'm trying to use Apache Syncope and want to use maven artifact like read hear:

https://syncope.apache.org/docs/reference-guide.html#client-library

but when I add this lines:

<dependency>
  <groupId>org.apache.syncope.client</groupId>
  <artifactId>syncope-client-lib</artifactId>
  <version>2.1.2</version>
</dependency>

To the pom.xml in my proyect in Eclipse. Only add this lines, Do not do anything else, and then the rest response changes to:

jsonMetadata: {status: "0", errorMessage: ""}
result: [{id: "4010", name: "Demo"}]

For me is a problem because I manage the errors whit this 'METADATA' word.

Does anyone know why this change occurs?

1

There are 1 best solutions below

3
On

In this case you define @XmlElement(name = "METADATA") only in for the first one field JsonMetadata. Remember Java only get this annotation to the first field under it!

When i create xml i prefer to use the notation in their get method, for example:

@XmlRootElement(name = "root")  
public class RestResponse<T> {


@XmlElement(name = "metadata")
public JsonMetadata getJsonMetadata() {
    return jsonMetadata;
}

public void setJsonMetadata(JsonMetadata jsonMetadata) {
    this.jsonMetadata = jsonMetadata;
}

public void setResult(<any> result) {
    this.result = result;
}

@XmlElement(name="result")
public <any> getResult() {
    return result;
}

REMEMBER: you have to create both setter and getter for each field! with the correct name (I use netbeans ide and it suggest automatically to add this method with the correct name).

BUT there is another solution...

@XmlAccessorType(XmlAccessType.FIELD)
public class RestResponse<T> {
@XmlElement(name = "METADATA")
private JsonMetadata  jsonMetadata;
private Collection<T> result;
//...

with this notation before the class you shuld risolve your problem. So there are 2 way: -add the method (i prefer this one) -add this notatio (witout add or touch anything, i don't like because the method are more usefull)

it is not possible to use the two solutions together!