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:
@Path("myResource") public class MyResource { @GET @Produces(MediaType.APPLICATION_JSON) 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 = client.target().path("myResource").request("application/json").get(Map.class);
With Jackson (as opposed to MOXy), you need to register the JacksonFeature manually, e.g. in your javax.ws.rs.core.Application 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:
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>...</version> </dependency>
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.