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");"这段代码,输出就不会有乱码。


源码稍后奉上

转载于:https://my.oschina.net/erichsbc/blog/159799

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值