restful webservice实战

上篇文章介绍了什么是restful风格的webservice,本片文章通过一个demo着重介绍如何发布一个restful风格的web service.

1.建立接口


@Path(value = "/sample")
public interface RESTSample {

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String doGet();

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/request/{param}")
	public String doRequest(@PathParam("param") String param,@Context HttpServletRequest servletRequest,
			@Context HttpServletResponse servletResponse);

	@GET
	@Path("/bean/{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public User getBean(@PathParam("id") int id);
}

2.建立实现类


@Path(value = "/sample")
public class RESTSampleSource implements RESTSample {

	@Context
	private UriInfo uriInfo;

	@Context
	private Request request;

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String doGet() {
		return "this is get rest request";
	}

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/request/{param}")
	public String doRequest(@PathParam("param") String param,@Context HttpServletRequest servletRequest,
			@Context HttpServletResponse servletResponse) {
		System.out.println(servletRequest);
		System.out.println(servletResponse);
		System.out.println(servletRequest.getParameter("param"));
		System.out.println(servletRequest.getContentType());
		System.out.println(servletResponse.getCharacterEncoding());
		System.out.println(servletResponse.getContentType());
		return "success";
	}

	@GET
	@Path("/bean/{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public User getBean(@PathParam("id") int id) {
		System.out.println("####getBean#####");
		System.out.println("id:" + id);
		System.out.println("Method:" + request.getMethod());
		System.out.println("uri:" + uriInfo.getPath());
		System.out.println(uriInfo.getPathParameters());
		User user = new User();
		user.setId(id);
		user.setName("JojO");
		return user;
	}
}

3.配置配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
	<bean id="restSample" class="com.hoo.service.RESTSampleSource"/>
	<!-- 这里的地址很重要,客户端需要通过这个地址来访问WebService -->
	<jaxrs:server id="restServiceContainer" address="/rest">
	    <jaxrs:serviceBeans>
	        <ref bean="restSample" />
	    </jaxrs:serviceBeans>
	    <jaxrs:extensionMappings>
	        <entry key="json" value="application/json" />
	        <entry key="xml" value="application/xml" />
	    </jaxrs:extensionMappings>
	    <jaxrs:languageMappings>
	           <entry key="en" value="en-gb"/>  
	    </jaxrs:languageMappings>
	</jaxrs:server>
</beans>

下面在浏览器中输入:http://localhost:8080/webservice,当出现如下效果时说明发布成功:

然后输入网址:http://localhost:8080/webservice/rest/sample/bean/123访问一下,看效果:

当出现上面的效果说明调用成功。


  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值