Lighting@刘迎光
相信了,才有可能遇见,不相信,也许只会是擦肩而过!
Toggle navigation
Lighting@刘迎光
首页
IT技术
微服务(IT)
技术问答
OpenBI
读书笔记
公众号【今日脑图】
关于我
自媒体
归档
标签
java-xwiki restful接口简单测试
java
xwiki
restful
2017-07-19 21:22:41
977
lightingfire
java
xwiki
restful
## xwiki测试流程 1. 测试页面 ``` public void normalTest()throws Exception, IOException{ CloseableHttpClient httpclient = HttpClients.createDefault(); JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb"); Unmarshaller unmarshaller = context.createUnmarshaller(); HttpUriRequest httpPost = RequestBuilder.get() .setUri(new URI("http://172.16.200.220:8082/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome")) .setHeader("Accept", "application/xml").build(); CloseableHttpResponse response = httpclient.execute(httpPost); System.out.println(response.getEntity()); HttpEntity responseEntity=response.getEntity(); Page page = (Page) unmarshaller.unmarshal(responseEntity.getContent()); System.out.println(new Gson().toJson(page)); } ``` 2. 登录并获取session ``` public void testLogin() throws Exception{ CloseableHttpClient httpclient = HttpClients.createDefault(); JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb"); HttpUriRequest httpPost = RequestBuilder.post() .setUri(new URI("http://172.16.200.220:8082/xwiki/bin/loginsubmit/XWiki/XWikiLogin")) .addParameter("j_username", "superadmin") .addParameter("j_password", "system") .setHeader("Accept", "application/xml").build(); CloseableHttpResponse response = httpclient.execute(httpPost); System.out.println(response.getStatusLine().getStatusCode()); HttpEntity responseEntity=response.getEntity(); System.out.println(responseEntity); cookieStr = response.getFirstHeader("Set-Cookie").getValue(); System.out.println(cookieStr); } ``` 3. 创建wiki页面(注意URL的最后一项,NewPage表示的是创建的页面标识,不同的页面此值应不同,否则会覆盖) ``` public void testNewPage() throws Exception{ Page page =new Page(); page.setTitle("testPage"); page.setSyntax("xwiki/2.0"); page.setContent("This is a testPage"); page.setId("xwiki:Main.testPage"); CloseableHttpClient httpclient = HttpClients.createDefault(); JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb"); Marshaller marshaller=context.createMarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller(); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal( page, out ); InputStreamEntity inputStreamEntity=new InputStreamEntity(new ByteArrayInputStream(out.toByteArray())); HttpUriRequest httpPost = RequestBuilder.put() .setUri(new URI("http://172.16.200.220:8082/xwiki/rest/wikis/xwiki/spaces/Main/pages/NewPage")) .setEntity(inputStreamEntity) .setHeader("Content-Type", "application/xml") .setHeader("Cookie", cookieStr).build(); CloseableHttpResponse response = httpclient.execute(httpPost); System.out.println(response.getStatusLine().getStatusCode()); HttpEntity responseEntity=response.getEntity(); System.out.println(responseEntity); if(response.getStatusLine().getStatusCode()<400){ Page responsePage = (Page) unmarshaller.unmarshal(responseEntity.getContent()); System.out.println(new Gson().toJson(responsePage)); } } ``` 4. 修改wiki,使用如上第三步方式,只要url不变,再提交都是修改,只有第一次提交是创建 ---------- > by 刘迎光@萤火虫工作室 > OpenBI交流群:495266201 > MicroService 微服务交流群:217722918 > mail: liuyg#liuyingguang.cn > 博主首页(==防止爬虫==):http://blog.liuyingguang.cn
Pre:
Docker1.10.1以上版本配置http API访问
Next:
HttpClient 传输xml字符串
Table of content