java - webservices - jersey return arraylist json

Jersey/JAX-RS: Return a Map as XML/JSON (4)

It's not so obvious how to return a Map as an XML/JSON document using the Jersey/JAX-RS framework. It already has support for Lists, but when it comes to Maps, there is no MessageBodyWriter. And even if I were to embed the Ma into a wrapper class, there is no map type in XML schema.

Any practical advice on how to marshal a Map into an XML/JSON document in Jersey?

By default, Jersey uses MOXy XML/JSON provider. So if you want to use MOXy, you need to add separate adapter. The simple solution is, remove MOXy dependency from pom.xml and add Jackson dependency. Jackson will take care of everything.

    <version>{latest version}</version>

Sorry,I'm not sure why MOXy is the default provider in Jeresey.

public class BeanClass{
    private int duration, Id;
    private String description;
    private User user = new User();
    Map<String, String> map = new HashMap<>();

@Produces({ MediaType.APPLICATION_JSON }) // , MediaType.APPLICATION_XML
public Response getAll() {
        List<BeanClass> lstBean = ...
        return Response.ok().entity(lstBean).build();


I know it's been a while already, but maybe someone will find it useful.

I am having a similar problem and to me it looks like, that only Jackson currently supports this sort of direct Map to JSON mapping.

In Jersey, it is as easy as returning the map from the resource method:

public class MyResource {
  public Map<String, String> getMap() {
      Map<String, String> map = new HashMap<String, String>();
      map.put("some key", "some value");
      return map;

and accessing it from the client as:

// ... (client initialization)
Map<String, String> map ="myResource").request("application/json").get(Map.class);

With Jackson (as opposed to MOXy), you need to register the JacksonFeature manually, e.g. in your subclass (or ResourceConfig in Jersey):

public class MyApp extends ResourceConfig {
  public MyApp() {
    super(MyResource.class, JacksonFeature.class);

And be sure to have Jersey Jackson module on classpath. In maven, simply add:


without maven, it might be a bit more tricky to add all the dependencies. And that's all, this should work. At least with Jackson provider.

It did not work for me in Jettison and id does not work in MOXy yet (there's an issue open).

Hope this helps.

It is very easy to do this in Java EE 7.

The REST Resource class:


import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.RequestScoped;

public class MapToJson {

    public Map getJson() {
       Map<String, String> theMap = new HashMap<>();
       theMap.put("foo", "bar");
       return theMap;

The Application:


import java.util.Set;
public class ApplicationConfig extends Application {

    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        // following code can be used to customize Jersey 2.0 JSON provider:
        try {
            Class jsonProvider = Class.forName("org.glassfish.jersey.jackson.JacksonFeature");
            // Class jsonProvider = Class.forName("org.glassfish.jersey.moxy.json.MoxyJsonFeature");
            // Class jsonProvider = Class.forName("org.glassfish.jersey.jettison.JettisonFeature");
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
        return resources;

    private void addRestResourceClasses(Set<Class<?>> resources) {

As you can see, you can configure different classes as jsonProvider:






The solution that worked for me (Using jettison):

public Response getJson() {
    Map<String, String> testMap = new HashMap<>();
    testMap.put("Key1", "value1");
    testMap.put("key2", "value2");
    JSONObject obj = new JSONObject(testMap);

    return Response.status(200).entity(obj).build();