2
//Main service 

@Path("/test")
public class ReturnMultiple {

    public static ArrayList<String> al = new ArrayList<String>();

    @POST
    @Path("/new/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public ArrayList<String> display(@PathParam("name") String name) {
        al.clear();
        Todo td = new Todo();
        td.setName(name);
        al.add(td.getName());
        return al;
    }
}

// This is Pojo

public class Todo {

    private String name;

    public void setName(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

}

Whenever I hit the service I get this error as Below

A message body writer for Java class java.util.ArrayList, and Java type java.util.ArrayList, and MIME media type text/plain was not found

6
  • This static arraylist is not a good idea, what happen if two requests for /test/new/name arrive at the same time? Commented Jan 7, 2017 at 6:49
  • Regarding the error message, the framework used don't know how to make text from an ArrayList, that's the issue Commented Jan 7, 2017 at 6:50
  • What to write in @produces ? Commented Jan 7, 2017 at 6:52
  • MediaType.XML or MediaType.JSON (something like that) Commented Jan 7, 2017 at 6:53
  • when i use Application_Json in @produces it give me this error A message body writer for Java class java.util.ArrayList, and Java type java.util.ArrayList<java.lang.String>, and MIME media type application/json was not found Commented Jan 7, 2017 at 6:54

2 Answers 2

1

You can't use ArrayList and produce TEXT_PLAIN. You'd have to use JSON. To setup JSON message body write in jax-rs you need to supply a @Provider for an ObjectMapper. Here's an example:

Sign up to request clarification or add additional context in comments.

Comments

0

I guess, you need a mapper implementation which helps in the serialization and deserialization. Without that it doesn't know how to convert an arraylist into text/plain or application/json or for that matter any other MIME type.

if you are dealing with application/json, jackson library works as really good mapper. It has messagebodyreader and messagebodywriter for you to do the difficult job.

you might have a way to provide a mapper for your rest services (as a provider), in some implementation it picks itself if you use jackson jar which I remember while using resteasy.

5 Comments

I am new to this , may you please write a code for mapper ? Thanks
In most cases when you include the jackson jar is automatically get registered github.com/FasterXML/jackson-jaxrs-providers as the link says so code won't be required probably, otheriwse here is an example of it with jersey mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson
I have included those jars and wrote like below : @POST @Path("/new/{name}") @Produces(MediaType.APPLICATION_JSON) public ArrayList<String> display(@PathParam("name") String name) { al.clear(); Todo td = new Todo(); td.setName(name); al.add(td.getName()); return al; }
still you face the same issue? I am not sure what are you using jor jax-rs? is it jersey, resteasy, cxf, restlet, can you list down your jar in the question?
jax-rs , i used Gson , and i am able to get the output , but now i want to know how can i put this arraylist and two boolean values in a hashmap and return the hashmap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.