1 2 3 4 5 | @Provider @Produces(value = { "application/json_grid" }) public class JsonGridProvider implements MessageBodyWriter<DojoList> { ... } |
1 2 3 4 | public boolean isWriteable(Class<?> inputClass, Type type, Annotation[] annotations, MediaType mediaType) { return inputClass == DojoList.class; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //Create a new instance JAXbContext on given context path. jc = JAXBContext.newInstance("rest.resource"); OutputStream tempOs = new ByteArrayOutputStream(); Marshaller ms = jc.createMarshaller(); HashMap<String, String> namespaceMap = new HashMap<String, String>(); namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", “xmlns”); namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xmlns.type"); System.out.println("Size is "+namespaceMap.size()); XMLOutputFactory factory = new MappedXMLOutputFactory(namespaceMap); XMLStreamWriter xsw = factory.createXMLStreamWriter(tempOs); ms.marshal(list, xsw); |
1 2 3 4 5 6 7 | {"dojoItems":{"items":[{"@xmlns.type.type":"employee","id":"001", "name":"Manager","manager":true,"sex":"male","age":35}, {"@xmlns.type.type":"employee","id":"002", "name":"developer","manager":false,"sex":"male","age":25}], "endRecordIndex":2, "totalRecords":2, "startRecordIndex":1}} |
1 2 3 4 5 6 7 8 | private String replaceAll(String jsonString) { String replacePattern = "\"@xmlns.type[a-z,A-Z,:,.,\"]*?,”; Pattern pattern = Pattern.compile(replacePattern); Matcher matcher = pattern.matcher(jsonString); return matcher.replaceAll(“”); } |
1 2 3 4 5 | {"dojoItems":{"items":[{"id":"001","name":"Manager","manager":true,"sex":"male","age":35}, {"id":"002","name":"developer","manager":false,"sex":"male","age":25}], "endRecordIndex":2, "totalRecords":2, "startRecordIndex":1}} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | int index = replacedJsonString.indexOf(":"); int end = replacedJson-String.lastIndexOf("}"); if (replacedJsonString.indexOf("items") > 0) { if (index > 0) { /*If size is equal to 1, then square brackets will not appear, so add square brackets */ if(list.getItems().size() == 1) { /*extracting json grid format*/ String itemsString = replacedJsonString.substring(index + 1, end); debug(methodName, "jsonString itemsString=" + itemsString); os.write(replaceWithParenthesisForSingleItem(itemsString,list).getBytes()); } else { /*extracting json grid format and writing to outputStream*/ os.write(replacedJsonString.substring(index + 1, end) .getBytes()); } } else { /*writing the replacedJsonString into outputStream*/ os.write(replacedJsonString.getBytes()); } } else { // For an empty list, items tag won’t be present. // But dojoWidget Requires it so adding it here. String emptJsonString = “{items:[],startRecordIndex:" + "0" • ", endRecordIndex:" + "0" + ",totalRecords:" + "0" + "}"; os.write(emptJsonString.getBytes()); } os.close(); xsw.close(); tempOs.close(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | private String replaceWithParenthesisForSingleItem(String jsonString,DojoList list) { final String methodName = "replaceWithParenthesisForSingleItem"; int firstColonIndex = jsonString.indexOf(":"); int endRecordIndex = jsonString.indexOf("endRecordIndex"); int flowerBracketBeforeEndRecordIndexString = jsonString .lastIndexOf("}",endRecordIndex); String jsonObject = jsonString.substring(firstColonIndex + 1, flowerBracketBeforeEndRecordIndexString + 1); String replaceString = "{items:[" + jsonObject + "],startRecordIndex:" • list.getStartRecordIndex() + ", endRecordIndex:" + list.getEndRecordIndex() + ", totalRecords:" + list.getTotalRecords()+ "}"; System.out.println("jsonString after replace=" + replaceString); return replaceString; } |
1 2 3 | {"items":[{"id":"001","name":"Manager","manager":true,"sex":"male","age":35}, {"id":"002","name":"developer","manager":false,"sex":"male","age":25}], "endRecordIndex":2,"totalRecords":2,"startRecordIndex":1} |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |