Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mycompany.maptojson.rest;
- import java.util.HashMap;
- import java.util.Map;
- import javax.ws.rs.Path;
- import javax.ws.rs.GET;
- import javax.ws.rs.Produces;
- import javax.enterprise.context.RequestScoped;
- @Path("maps")
- @RequestScoped
- public class MapToJson {
- @GET
- @Produces("application/json")
- public Map getJson() {
- Map<String, String> theMap = new HashMap<>();
- theMap.put("foo", "bar");
- return theMap;
- }
- }
- package com.mycompany.maptojson.rest;
- import java.util.Set;
- import javax.ws.rs.core.Application;
- @javax.ws.rs.ApplicationPath("webresources")
- public class ApplicationConfig extends Application {
- @Override
- 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");
- resources.add(jsonProvider);
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- addRestResourceClasses(resources);
- return resources;
- }
- private void addRestResourceClasses(Set<Class<?>> resources) {
- resources.add(com.mycompany.maptojson.rest.MapToJson.class);
- }
- }
- Class.forName("org.glassfish.jersey.jackson.JacksonFeature");
- Class.forName("org.glassfish.jersey.moxy.json.MoxyJsonFeature");
- Class.forName("org.glassfish.jersey.jettison.JettisonFeature");
- @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;
- }
- }
- // ... (client initialization)
- Map<String, String> map = client.target().path("myResource").request("application/json").get(Map.class);
- public class MyApp extends ResourceConfig {
- public MyApp() {
- super(MyResource.class, JacksonFeature.class);
- }
- }
- <dependency>
- <groupId>org.glassfish.jersey.media</groupId>
- <artifactId>jersey-media-json-jackson</artifactId>
- <version>...</version>
- </dependency>
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- 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();
- }
- <dependency>
- <groupId>org.glassfish.jersey.media</groupId>
- <artifactId>jersey-media-json-jackson</artifactId>
- <version>{latest version}</version>
- </dependency>
- public class BeanClass{
- private int duration, Id;
- private String description;
- private User user = new User();
- Map<String, String> map = new HashMap<>();
- }
- @GET
- @Produces({ MediaType.APPLICATION_JSON }) // , MediaType.APPLICATION_XML
- public Response getAll() {
- List<BeanClass> lstBean = ...
- return Response.ok().entity(lstBean).build();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement