Jboss RestEasy构建简单的RESTful Web Services示例(2)--p...

上一篇博文说道了RestEasy构建简单的Webservice,举了一个“helloworld”的示例,直接在网址上输入URL就可调用服务,这个"helloworld"的示例只传一个参数,如果需要传递多个参数或是一堆的字符串,在URL上实现显得有点不现实,而且会有不少的问题,比如空白字符,特殊字符等。下面主要介绍一下用Resteasy来构建提交对象的Webservice。

首先构建一个对象

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,object,post,resteasy)