0

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?

Juan Medina
  • 164
  • 11

1 Answers1

0

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!

  • Thanks a lot Amith, but I'm still in problems. Looks like the problem is with the class RestResponse but I try to do both solutions and the first still send me the jsonMetadata insteed of METADATA... and with the second one Eclipse give me an error: XmlAccessType cannot be resolved to a variable. ... Maybe I can post my code, but here I can't :-( – Juan Medina Nov 30 '18 at 18:51
  • Now i think that the problem is with the Apache Syncope, i don't use it (i usually use other) maybe the problem could be some declaration or import. Your class is ok,! Maybe you have to add some propieties in your pom file. For example I put this in mine, (i use maven) UTF-8 1.8 1.8 – Amith Greca Dec 01 '18 at 10:43
  • Yes, looks like the problem is with Apache Syncipe.... I hope someone to use can help me, because I use your properties but still don't works. – Juan Medina Dec 02 '18 at 17:46