Jboss RestEasy構建簡單的RESTful Web Services示例(2)--p...

上一篇博文說道了RestEasy構建簡單的Webservice,舉了一個「helloworld」的示例,直接在網址上輸入URL就可調用服務,這個"helloworld"的示例只傳一個參數,若是須要傳遞多個參數或是一堆的字符串,在URL上實現顯得有點不現實,並且會有很多的問題,好比空白字符,特殊字符等。下面主要介紹一下用Resteasy來構建提交對象的Webservice。 html

首先構建一個對象 java

package com.hsbc.resteasy;

public class Issue {

	private String projectName;
	private String issueType;
	private String description;
	private String summary;
	private String enviroment;
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	public String getIssueType() {
		return issueType;
	}
	public void setIssueType(String issueType) {
		this.issueType = issueType;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getEnviroment() {
		return enviroment;
	}
	public void setEnviroment(String enviroment) {
		this.enviroment = enviroment;
	}

	
	@Override
	public String toString() {
		return "Issue [projectName=" + projectName + ", issueType=" + issueType + ",description="+description+",summary="+summary+",enviroment="+enviroment+"]";
	}
}
定義服務,其中參數" @Consumes("application/json") "爲輸入參數的格式,這裏爲JSON
@POST
 @Path("/postIssue")
 @Consumes("application/json")
 public Response postIssue(Issue issue) {
 String result = "Issue created : " + issue;
 return Response.status(201).entity(result).build();
 }
測試代碼
public static void main(String[] args) {
		// TODO Auto-generated method stub
		String projectName = "FTP";
		String issueType = "1";
		String enviroment = "TEST-ENVIROMENT";
		String summary = "TEST-SUMMARY";
		String description = "TEST-ENVIROMENT\\\\"//'\\\\' means '\'

				+ "如今市場上惟一的下一代遊戲主機 Wii U 的美好時光即將走到盡頭,其。";
		try {

			ClientRequest request = new ClientRequest(
					"http://localhost:8080/resteasyExample/rest/message/postIssue");
			request.accept("application/json");
			request.accept("text/html;charset=UTF-8");
			String input = "{\"projectName\":\"" + projectName
					+ "\",\"issueType\":\"" + issueType
					+ "\",\"description\":\"" + description
					+ "\",\"summary\":\"" + summary + "\",\"enviroment\":\""
					+ enviroment + "\"}";

			request.body("application/json;charset=UTF-8", input);
			ClientResponse<String> response = request.post(String.class);		
			//System.out.println(response.getStatus());
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new ByteArrayInputStream(response.getEntity().getBytes())));

			String output;
			//System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {

				System.out.println("IssueKey:"+output);
			}

		} catch (MalformedURLException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

這裏要注意的是中文編碼, request.body("application/json;charset=UTF-8", input);," charset=UTF-8"必須添加在這個request.body中,提交的時候就不會出現錯誤, 添加"request.accept("text/html;charset=UTF-8");"這段代碼,輸出就不會有亂碼。 json


源碼稍後奉上 app

相關文章
相關標籤/搜索