java restful 服务端搭建

java restful 服务端搭建 ,

jar

  1. asm-3.1.jar
  2. jersey-client-1.17.1.jar
  3. jersey-core-1.17.1.jar
  4. jersey-server-1.17.1.jar
  5. jersey-servlet-1.17.1.jar
  6. jsr311-api-1.1.1.jar
web.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp


Jersey REST Service
com.sun.jersey.spi.container.servlet.ServletContainer

com.sun.jersey.config.property.packages
com.control

1


Jersey REST Service
/rest/*


代码

package com.control;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.sf.json.JSONObject;
/**
 * http://www.importnew.com/7336.html 參考
 * @author jsonv_000 
 *
 */
// 这里@Path定义了类的层次路径。
// 指定了资源类提供服务的URI路径。
@Path("UserInfoService")
public class UserInfo {

		// @GET表示方法会处理HTTP GET请求
		@GET
		// 这里@Path定义了类的层次路径。指定了资源类提供服务的URI路径。
		@Path("/name/{i}")
		// @Produces定义了资源类方法会生成的媒体类型。
		@Produces(MediaType.TEXT_HTML)
//		@Produces({"application/xml", "application/json"})
		// @PathParam向@Path定义的表达式注入URI参数值。
		public String userName(@PathParam("i") String i) {
//			JSONObject jsonObject =new JSONObject();
//			String name = i;
//			jsonObject.put("code", ""+name);
//			//return "" + "" + name + "" + "";
			return "{'invokemethod':'sucess','postmethod','true'}";
//			return jsonObject.toString();
		}

		@GET
		@Path("/age/{j}")
		@Produces(MediaType.TEXT_XML)
		public String userAge(@PathParam("j") int j) {

			int age = j;
			return "" + "" + age + "" + "";
		}
}
访问:

http://localhost:8080/restful/rest/UserInfoService/name/Pavithrajiaxi

显示:

{'invokemethod':'sucess','postmethod','true'}



参考资料

http://www.importnew.com/7336.html


你可能感兴趣的:(javaee,服务端,rest,restful服务端)