1 2 3 4 5 | <bean id="atomConverter" class="org.springframework.http.converter.feed .AtomFeedHttpMessageConverter"> <property name="supportedMediaTypes" value="application/atom+xml" /> </bean> |
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 | @RequestMapping(method=RequestMethod.GET, value="/emps", headers="Accept=application/atom+xml") public @ResponseBody Feed getEmpFeed() { List<Employee> employees = employeeDS.getAll(); return AtomUtil.employeeFeed(employees, jaxb2Mashaller); } public static Feed employeeFeed( List<Employee> employees, Jaxb2Marshaller marshaller) { Feed feed = new Feed(); feed.setFeedType("atom_1.0"); feed.setTitle("Employee Atom Feed"); List<Entry> entries = new ArrayList<Entry>(); for(Employee e : employees) { StreamResult result = new StreamResult( new ByteArrayOutputStream()); marshaller.marshal(e, result); String xml = result.getOutputStream().toString(); Entry entry = new Entry(); entry.setId(Long.valueOf(e.getId()).toString()); entry.setTitle(e.getName()); Content content = new Content(); content.setType(Content.XML); content.setValue(xml); List<Content> contents = new ArrayList<Content>(); contents.add(content); entry.setContents(contents); entries.add(entry); } feed.setEntries(entries); return feed; } |
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 | <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> <title>Employee Atom Feed</title> <entry> <title>Huang Yi Ming</title> <id>1</id> <content type="xml"> <employee> <email>huangyim@cn.ibm.com</email> <id>1</id> <name>Huang Yi Ming</name> </employee> </content> </entry> <entry> <title>Wu Dong Fei</title> <id>2</id> <content type="xml"> <employee> <email>wudongf@cn.ibm.com</email> <id>2</id> <name>Wu Dong Fei</name> </employee> </content> </entry> </feed> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @RequestMapping(method=RequestMethod.POST, value="/emp") public @ResponseBody Employee addEmp(@RequestBody Employee e) { employeeDS.add(e); return e; } @RequestMapping(method=RequestMethod.PUT, value="/emp/{id}") public @ResponseBody Employee updateEmp( @RequestBody Employee e, @PathVariable String id) { employeeDS.update(e); return e; } @RequestMapping(method=RequestMethod.DELETE, value="/emp/{id}") public @ResponseBody void removeEmp(@PathVariable String id) { employeeDS.remove(Long.parseLong(id)); } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |