restlet基于 jax-ws的测试例子

1.使用jar包截图

restlet基于 jax-ws的测试例子_第1张图片

2.RestJaxRsServer代码如下:


package org.cosco.restlet;

import org.cosco.restlet.application.MyApplication;
import org.cosco.restlet.application.RestJaxRsApplication;
import org.restlet.Component;
import org.restlet.data.Protocol;
import org.restlet.ext.jaxrs.JaxRsApplication;

public class RestJaxRsServer {

	public static void main(String[] args) throws Exception
	{
		Component component = new Component();
		component.getServers().add(Protocol.HTTP, 8082);
		component.getDefaultHost().attach(new JaxRsApplication(new MyApplication()));
		component.start();
		System.out.println("the restlet server start...");
	}
	
}



3.MyApplication代码如下:



package org.cosco.restlet.application;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import org.cosco.restlet.resource.MyResource;

public class MyApplication extends Application {

	@Override
	public Set<Class<?>> getClasses() {
		// TODO Auto-generated method stub
		
		Set<Class<?>> resources		= new HashSet<Class<?>>();
		resources.add(MyResource.class);
		
		return resources;
	}
	
}


4.MyResource代码如下:


package org.cosco.restlet.resource;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.restlet.data.Form;
import org.restlet.representation.Representation;

@Path("/")
public class MyResource{

	@GET
	@Path("{id}/json")
	@Produces("application/json")
	public String getjson(@PathParam("id") int id)
	{
		return String.valueOf(id);
	}
	
	@GET
	@Path("{id}/xml")
	@Produces("application/json")
	public String getxml(@PathParam("id") int id)
	{
		return String.valueOf(id);
	}
	
	@POST
	@Path("add")
	public String add(Representation entity)
	{
		Form form 		= new Form(entity);
		
		String name 	= form.getFirstValue("name");
		
		return name;
		
	}
	
}

5.浏览器访问 http://127.0.0.1:8082/1/json 输出成功。控制打印如下:


你可能感兴趣的:(webservice,Restlet,jax-ws)