1

I have a method which is returning Map<String, ArrayList<EntityClass>>. code is as follow of Definition class:

public Map<String, ArrayList<EntityClass>> webMethod1(){
    ArrayList<EntityClass> arr = new ArrayList<>();
    for (int i=0;i<5;i++){
        arr.add(new EntityClass(i, String.valueOf(i)));
    }
    Map<String, ArrayList<EntityClass>> map = new HashMap<>();
    map.put("Entity", arr);
}

Further this is called by a web service as follow:

@GET
@Path("/m1")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, ArrayList<EntityClass>> m1(){
    return new Definition().webMethod1();
}

but i am getting following on the console:

MessageBodyWriter not found for media type=application/json, type=class java.util.HashMap, genericType=class java.util.HashMap.

and HTTP 500 as error.

how to resolve this error

Stark
  • 481
  • 1
  • 9
  • 31

1 Answers1

1

May be you should consider using a simple marshaller library like GSON. I end up with this code :

@GET
@Path("/m1")
@Produces(MediaType.APPLICATION_JSON)
public String webMethod1(){
    ArrayList<EntityClass> arr = new ArrayList<>();
    for (int i=0;i<5;i++){
        arr.add(new EntityClass(i, "\"-'"+String.valueOf(i)));
    }
    Map<String, List<EntityClass>> map = new HashMap<>();
    map.put("Entity", arr);

    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();

    return gson.toJson(map);
}

This does the job pretty well to me and don't add a lot of complexity as writing a MessageBodyWriter which is pointless for simple POJO classes and structures.

TrapII
  • 2,219
  • 1
  • 15
  • 15