Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

web services - Getting error A message body writer for Java class java.util.ArrayList/List<java.lang.String> was not found

well this has been posted a lot of time here but no solution worked for me...
i can avoid this error by making a wrapper class but it only returned

</stringWrapper>

what am i doing wrong ?

StringWrapper class:

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class StringWrapper {
    public StringWrapper (){}

    List<String> list=new ArrayList<String>();

    public void add(String s){ 
        list.add(s);
    }
}

code :

     @Path("/xml")
     @GET
     @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
     public StringWrapper mystring(){
        StringWrapper thestring=new StringWrapper();
        thestring.add("a");
        thestring.add("a");
        return thestring;
     }

Java Rest webservice using Jersey.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should add at least a getter in your StringWrapper. Without public attributes, there is nothing to serialize. So the output is correct. If you do not want a tag around the list of your strings, you can mark a single getter with @XmlValue.

@XmlRootElement
public class StringWrapper {
    public StringWrapper (){}

    List<String> list=new ArrayList<String>();

    public void add(String s) { list.add(s); }

    @XmlValue    
    public List<String> getData() { return list; }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...