Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. package com.mycompany.maptojson.rest;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.GET;
  7. import javax.ws.rs.Produces;
  8. import javax.enterprise.context.RequestScoped;
  9.  
  10. @Path("maps")
  11. @RequestScoped
  12. public class MapToJson {
  13.  
  14. @GET
  15. @Produces("application/json")
  16. public Map getJson() {
  17. Map<String, String> theMap = new HashMap<>();
  18. theMap.put("foo", "bar");
  19. return theMap;
  20. }
  21. }
  22.  
  23. package com.mycompany.maptojson.rest;
  24.  
  25. import java.util.Set;
  26. import javax.ws.rs.core.Application;
  27.  
  28. @javax.ws.rs.ApplicationPath("webresources")
  29. public class ApplicationConfig extends Application {
  30.  
  31. @Override
  32. public Set<Class<?>> getClasses() {
  33. Set<Class<?>> resources = new java.util.HashSet<>();
  34. // following code can be used to customize Jersey 2.0 JSON provider:
  35. try {
  36. Class jsonProvider = Class.forName("org.glassfish.jersey.jackson.JacksonFeature");
  37. // Class jsonProvider = Class.forName("org.glassfish.jersey.moxy.json.MoxyJsonFeature");
  38. // Class jsonProvider = Class.forName("org.glassfish.jersey.jettison.JettisonFeature");
  39. resources.add(jsonProvider);
  40. } catch (ClassNotFoundException ex) {
  41. java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
  42. }
  43. addRestResourceClasses(resources);
  44. return resources;
  45. }
  46.  
  47. private void addRestResourceClasses(Set<Class<?>> resources) {
  48. resources.add(com.mycompany.maptojson.rest.MapToJson.class);
  49. }
  50. }
  51.  
  52. Class.forName("org.glassfish.jersey.jackson.JacksonFeature");
  53.  
  54. Class.forName("org.glassfish.jersey.moxy.json.MoxyJsonFeature");
  55.  
  56. Class.forName("org.glassfish.jersey.jettison.JettisonFeature");
  57.  
  58. @Path("myResource")
  59. public class MyResource {
  60. @GET
  61. @Produces(MediaType.APPLICATION_JSON)
  62. public Map<String, String> getMap() {
  63. Map<String, String> map = new HashMap<String, String>();
  64. map.put("some key", "some value");
  65. return map;
  66. }
  67. }
  68.  
  69. // ... (client initialization)
  70. Map<String, String> map = client.target().path("myResource").request("application/json").get(Map.class);
  71.  
  72. public class MyApp extends ResourceConfig {
  73. public MyApp() {
  74. super(MyResource.class, JacksonFeature.class);
  75. }
  76. }
  77.  
  78. <dependency>
  79. <groupId>org.glassfish.jersey.media</groupId>
  80. <artifactId>jersey-media-json-jackson</artifactId>
  81. <version>...</version>
  82. </dependency>
  83.  
  84. @GET
  85. @Produces(MediaType.APPLICATION_JSON)
  86. public Response getJson() {
  87. Map<String, String> testMap = new HashMap<>();
  88. testMap.put("Key1", "value1");
  89. testMap.put("key2", "value2");
  90. JSONObject obj = new JSONObject(testMap);
  91.  
  92. return Response.status(200).entity(obj).build();
  93. }
  94.  
  95. <dependency>
  96. <groupId>org.glassfish.jersey.media</groupId>
  97. <artifactId>jersey-media-json-jackson</artifactId>
  98. <version>{latest version}</version>
  99. </dependency>
  100.  
  101. public class BeanClass{
  102. private int duration, Id;
  103. private String description;
  104. private User user = new User();
  105. Map<String, String> map = new HashMap<>();
  106. }
  107.  
  108.  
  109. @GET
  110. @Produces({ MediaType.APPLICATION_JSON }) // , MediaType.APPLICATION_XML
  111. public Response getAll() {
  112. List<BeanClass> lstBean = ...
  113. return Response.ok().entity(lstBean).build();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement