tomcat7上部署RESTeasy[helloworld]

之前在项目上使用过Jersey实现的JAXRS RESTful Webservice部分交互功能,容器是tomcat;看到RESTful还有另一种实现resteasy,不过resteasy和jboss容器紧密结合,但它同时也支持jdk5以上的tomcat环境。


一,工具

tomcat7.0.68,

resteasy-jaxrs-3.0.17.Final,

EclipseJEE(Mars2)


二,RESTeasy服务端配置

参考官方的userguide文档,配置web.xml配置、实现javax.ws.rs.core.Application、实现资源类(merrick.resteasy.):

1,配置web.xml



  rest1

	
   	
		resteasy.logger.type
		LOG4J
	
	
	
           resteasy.resources
           merrick.resteasy.ServeResource
    
	
	
        resteasy.servlet.mapping.prefix
        /resteasy
     
   
   	
      
         org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
      
     
  
  	
        Resteasy
        
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        
        
            javax.ws.rs.Application
             
			merrick.resteasy.TestAPPApplication
        
    
    	
	
		Resteasy
		/resteasy/*
	


2,实现简单的资源服务类:

package merrick.resteasy;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
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.Context;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;

@Path("/rootclasspath")
public class ServeResource {
	@Context HttpServletRequest rq ;
	
	public static Logger log = Logger.getLogger(ServeResource.class);
	
	@GET
	@Path("/m1")
	@Produces(MediaType.TEXT_HTML)
	public String getstring1(){
		
		log.info("Get Request, " + new Date().toString());
		return "hello1";
	}
	
	@GET
	@Path("/m2/{param1}")
	@Produces(MediaType.TEXT_HTML)
	public String getstring2(@PathParam("param1") String p1 ){
		log.info("REQ URL: "+ rq.getRequestURL());
		log.info("Get Request: " + new Date().toString());
		log.info( p1);
		return "hello2";
	}

}

3,按照文档继承了Application

package merrick.resteasy;

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

import javax.ws.rs.core.Application;

public class TestAPPApplication extends Application {
	
    Set objectSet = new HashSet();  
    Set> classSet = new HashSet>(); 
    
    public TestAPPApplication() {
	
    	objectSet.add(new ServeResource());
	}
    
    @Override
    public Set> getClasses() {
    	
    	return classSet;
    }
    
    @Override
    public Set getSingletons() {
    	
    	return objectSet;
    }
   
   

}
 
  
三,tomcat7上调试运行

因为环境限制没有使用Maven,web项目中把resteasy-jaxrs-3.0.17.Final/lib里的所有jar包都引进了,于是带来了一堆问题,有两个问题网上搜了半天没答案,大约所有包都加载进来了一定有不需要的而导致问题的,尝试了几次,解决如下:

问题日志:
找不到类org/hibernate/validator/method/MethodValidator
解决方法:
删除所有有关hibernate或validator的jar引用

另一个问题:
404问题,没有其他错误日志
解决方法:
继承Application类

四,使用http客户端程序测试:

主要代码:

		CloseableHttpClient c = HttpClients.createDefault();
		
		String url1 = "http://localhost:8080/resteasyservertest1/resteasy/rootclasspath/m1";

		HttpGet g = new HttpGet(url1);
		RequestConfig defaultRequestConfig = RequestConfig.custom()
			    .setSocketTimeout(5000)
			    .setConnectTimeout(5000)
			    .setConnectionRequestTimeout(5000)			//超时时间必须要设,不然客户端会无限等待
			    .setStaleConnectionCheckEnabled(true)
			    .build();
		
		g.setConfig(defaultRequestConfig);
		CloseableHttpResponse  r = c.execute(g);
		System.out.println("Response: "+EntityUtils.toString(r.getEntity()));
		System.out.println("Code: " + r.getStatusLine().getStatusCode());
		
		r.close();
		c.close();	

运行结果:

服务端

[RestServ] INFO [http-bio-8080-exec-13] merrick.resteasy.ServeResource.getstring2(32) | REQ URL: http://localhost:8080/resteasyservertest1/resteasy/rootclasspath/m2/x1
[RestServ] INFO [http-bio-8080-exec-13] merrick.resteasy.ServeResource.getstring2(33) | Get Request: Sun Oct 09 15:59:48 CST 2016
[RestServ] INFO [http-bio-8080-exec-13] merrick.resteasy.ServeResource.getstring2(34) | x1


客户端

Response: hello2
Code: 200




你可能感兴趣的:(tomcat,WebService,Java)