rest入门实践之三:

第一步:建立webservice

 

 

第二步:后台代码:

复制代码
 1 @Path("hello")
 2 public class HelloXml {
 3 
 4     @GET
 5     @Path("getHelloXml")
 6     @Produces("application/xml")
 7     @Consumes("text/plain")
 8     public String getHelloXml(@DefaultValue("1") @QueryParam("id") String id) throws Exception {
 9         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
10         DocumentBuilder dBuilder = factory.newDocumentBuilder();
11         Document document = dBuilder.newDocument();
12         
13         
14         //模拟
15         Element root = document.createElement("湖北省");
16         
17         root.appendChild(document.createElement("武汉市"));
18         root.appendChild(document.createElement("襄樊市"));
19         
20         root.setAttribute("id", id);
21         
22         document.appendChild(root);
23         
24         //输出
25         StringWriter strWtr = new StringWriter();  
26         StreamResult strResult = new StreamResult(strWtr);  
27         TransformerFactory tfac = TransformerFactory.newInstance();  
28         Transformer t = tfac.newTransformer();  
29         t.transform(new DOMSource(document.getDocumentElement()),strResult);  
30         String result = strResult.getWriter().toString();  
31         System.out.println(result);  
32         return result;      
33     }
34 }
复制代码

 

 

第三步:启动服务: 访问:http://localhost/RestPractice3/services/hello/getHelloXml?id=4

 

第四步:结果:

posted on   pony1223  阅读(407)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端

导航

< 2012年6月 >
27 28 29 30 31 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
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示