edit readme and alter

Change-Id: I2da08c38890ee71bdd6ae9ece092bbc13cff0e2d
This commit is contained in:
rui.zhang 2018-12-21 10:56:56 +08:00
parent db87c84f40
commit 406f59ab0f
5 changed files with 75 additions and 11 deletions

View File

@ -69,7 +69,7 @@ WordPress实例是所有操作的基础。要创建WordPress实例我们需要
``` ```
(为了便于查看故用json包装了下返回结果) (为了便于查看故用json包装了下返回结果)
getAuthors()方法返回的主要是用户相关的信息: getAuthors()方法返回的是用户相关的信息:
```java ```java
List<Author> list = wp.getAuthors(); List<Author> list = wp.getAuthors();
``` ```
@ -91,9 +91,73 @@ getAuthors()方法返回的主要是用户相关的信息:
## 新增文章 ## 新增文章
新增文章可以使用newPost()方法,示例代码如下:
```java
PostRequest post = new PostRequest();
post.setPostTitle("测试PostName");
post.setPostContent("这是一段测试文章内容");
post.setCategories("测试");
post.setTags("a", "b", "c");
post.setPostName("test-post-name");
int postId = wp.newPost(post);
```
该方法的返回结果为postId即文章ID。
这里需要注意postName和postTitle。postTitle指的是文章标题postName指的则是文章别名主要在文章的url路径中使用。通常建议将postName设置为英文字符。
更多发布文章的参数可以参考[XML-RPC WordPress API/Posts - newPost](https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost)文档描述。
## 获取文章 ## 获取文章
获取文章有两个方法getPosts和getPost。前者用于获取多篇文章后者用于根据postId来获取文章。
### getPosts
getPosts方法如下
```java
List<Post> getPosts(PostFilter filter, String... fields)
```
参数filter决定返回结果的数量、排序字段和排序方式等信息。
变长参数fields则决定了返回结果中包含哪些字段。如果要返回全部字段可以不填。使用空字符串或其他非法字段则只返回postId。可用fields值请参考[XML-RPC WordPress API/Posts - getPost](https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#Return_Values)。
示例代码如下:
```java
PostFilter f = new PostFilter();
f.setNumber(2);
List<Post> list = wp.getPosts(f,"post_title");
```
返回结果为:
```json
[
{
"post_id": "2174",
"post_title": "sbt下载加速方案"
},
{
"post_id": "2170",
"post_title": "Java 中文字符按Unicode排序"
}
]
```
再次啰嗦下正常的返回结果是一个Post实例集合不是json字符串这里使用json字符串只是为了便于展示。如需要获取json结果集可以自行将结果集转为json也可以调用wp-client提供的JsonKit.toJson()进行处理。
### getPost
getPost方法如下
```java
Post getPost(int postId, String... fields)
```
通过postId获取对应文章的信息。变长参数fields的使用请参考getPosts方法。
## 编辑文章 ## 编辑文章
## 删除文章 ## 删除文章

View File

@ -63,9 +63,9 @@ class WPClient {
} }
String newPost(PostRequest post) throws XmlRpcException, IOException { int newPost(PostRequest post) throws XmlRpcException, IOException {
Object[] params = new Object[]{config.getBlogId(), config.getUsername(), config.getPassword(), post.toMap()}; Object[] params = new Object[]{config.getBlogId(), config.getUsername(), config.getPassword(), post.toMap()};
String postId = execute("wp.newPost", params); int postId = execute("wp.newPost", params, Integer.class);
return postId; return postId;
} }

View File

@ -79,7 +79,7 @@ public final class WordPress {
} }
public String newPost(PostRequest post) { public int newPost(PostRequest post) {
try { try {
return client.newPost(post); return client.newPost(post);
} catch (Exception e) { } catch (Exception e) {

View File

@ -11,7 +11,6 @@ import java.io.IOException;
/** /**
* @author robin * @author robin
*/ */
public abstract class JsonKit { public abstract class JsonKit {
private static final ObjectMapper MAPPER = new ObjectMapper(); private static final ObjectMapper MAPPER = new ObjectMapper();
@ -24,6 +23,7 @@ public abstract class JsonKit {
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_ABSENT); MAPPER.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
MAPPER.setDefaultPropertyInclusion(JsonInclude.Include.NON_DEFAULT);
} }

View File

@ -54,8 +54,8 @@ public class WordPressTest {
@Test @Test
public void getPosts() throws JsonProcessingException { public void getPosts() throws JsonProcessingException {
PostFilter f = new PostFilter(); PostFilter f = new PostFilter();
f.setNumber(30); f.setNumber(2);
List<Post> list = wp.getPosts(f); List<Post> list = wp.getPosts(f,"post_title");
System.out.println(toJson(list)); System.out.println(toJson(list));
Assert.assertFalse(list.isEmpty()); Assert.assertFalse(list.isEmpty());
} }
@ -71,12 +71,12 @@ public class WordPressTest {
@Test @Test
public void newPost() { public void newPost() {
PostRequest post = new PostRequest(); PostRequest post = new PostRequest();
post.setPostTitle("测试PostName" + System.currentTimeMillis()); post.setPostTitle("测试PostName");
post.setPostContent("这是一段测试内容" + System.currentTimeMillis()); post.setPostContent("这是一段测试文章内容");
post.setCategories("测试"); post.setCategories("测试");
post.setTags("a", "b", "c"); post.setTags("a", "b", "c");
post.setPostName("abczzdddd"); post.setPostName("test-post-name");
String postId = wp.newPost(post); int postId = wp.newPost(post);
System.out.println(postId); System.out.println(postId);
Assert.assertNotNull(postId); Assert.assertNotNull(postId);
} }