使用注解来让你的POJO完成restful风格的服务。

本demo基于jersry1.2

提供的服务类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.uninf.webprovider.action;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmsps.paladin.utils.iptools.IPUtil;
 
@Path(value = "/IPService")
public class IPAction {
	public final static Logger log = LoggerFactory.getLogger(IPAction.class);
 
	@GET
	@Path(value = "/location/{ip}")
	@Produces("text/plain;charset=UTF-8")
	public String getLocationByIP(@PathParam("ip") String ip) {
		log.info("{" + ip + "} request get location service");
		return IPUtil.getAddrByIP(IPUtil.getServerIP(ip));
	}
 
}

web.xml的配置

1
2
3
4
5
6
7
8
<servlet>
		<servlet-name>Jersey Web Application</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey Web Application</servlet-name>
		<url-pattern>/restful/*</url-pattern>
	</servlet-mapping>

and then you can vis http://localhost:8080/yourproject/restful/IPService/location/baidu.com

访问查看页面效果
baidu