RESTEasy - a simple string array / collector

Is there an easy way to sort and parse String [] or List in RESTEasy?

My sample code is:

@GET
@Path("/getSomething")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getSomeData() {
    return Arrays.asList("a","b","c","d");

}

Above is the Exception:

Could not find MessageBodyWriter for response object 
of type: java.util.Arrays$ArrayList of media type: application/json
+3
source share
2 answers

You may need to wrap it like this:

public List<JaxbString> getList(){
     List<JaxbString> ret= new ArrayList<JaxbString>();
     List<String> list = Array.asList("a","b","c");
          for(String s:list){
              ret.add(new JaxbString(s));
          }
     return ret;
}

@XmlRootElement
public class JaxbString {

    private String value;

    public JaxbString(){}

    public JaxbString(String v){
        this.setValue(v);
    }

    public void setValue(String value) {
        this.value = value;
    }

    @XmlElement
    public String getValue() {
        return value;
    }

}
+4
source

I have the same problem with XML and JSON. We have not found a solution yet, but I think it is related to JAXB.

So it turns out that the problem is that JAXB already comes with JDK6, and the dependencies on JBoss are wrong. They must find another solution for themselves, as is done now. Anyone how this can be solved:

<!-- JAXB Reasteasy support -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
<version>1.2.1.GA</version>
<scope>compile</scope>
<exclusions>
    <exclusion>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
    </exclusion>
    <exclusion>
        <groupId>com.sun.xml.stream</groupId>
        <artifactId>sjsxp</artifactId>
    </exclusion>
</exclusions>

You will get RESTEASY JAXB provider, but not maven JAXB files.

+1

Source: https://habr.com/ru/post/1746336/


All Articles