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
569 views
in Technique[技术] by (71.8m points)

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

Im using Jersey to build a REST Service and want to return a Collection<String> as XML.

@GET
@Produces(MediaType.TEXT_XML)
@Path("/directgroups")
public Response getDirectGroupsForUser(@PathParam("userId") String userId) {
    try {
        Collection<String> result = service.getDirectGroupsForUser(userId, null, true);

//      return result; //first try
//      return result.toArray(new String[0]); //second try
        return Response.ok().type(MediaType.TEXT_XML).entity(result).build(); //third try
    } catch (UserServiceException e) {
        LOGGER.error(e);
        throw new RuntimeException(e.getMessage());
    }
}

but my attempts fail with the following exception:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type text/xml was not found

and all results to that exception I found via google dealt with returning text/json instead of text/xml like in my situation.

Can anyone help me? I thought, if I use a Response, that would be my root element in XML and my collection a list of string elements in it..

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Use

List<String> list = new ArrayList<String>();
GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {};
Response response = Response.ok(entity).build();

The Generic entity wrapper works to get the output when using the Response builder.

Reference


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

...