RESTEasy Introduction
RESTEasy is an open-source project of JBoss. It provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a complete implementation of JAX-RS specifications and passes JCP certification. As a JBOSS project, it can be well integrated with the JBOSS application server. However, it can also run in any Servlet container that runs JDK 5 or a later version. RESTEasy also provides a RESTEasy JAX-RS client call framework. It can be easily integrated with EJB, Seam, Guice, Spring and Spring MVC. Supports automatic GZIP decompression on the client and server.
Based on the previous article, implement SpringJUnit4ClassRunner
[Java]
Package com. jje. common. test;
Import java. lang. annotation. Annotation;
Import java. lang. annotation. ElementType;
Import java. lang. annotation. Retention;
Import java. lang. annotation. RetentionPolicy;
Import java. lang. annotation. Target;
Import java. util. Collections;
Import java. util. List;
Import org. jboss. resteasy. plugins. server. tjws. TJWSEmbeddedJaxrsServer;
Import org. junit. runner. notification. RunNotifier;
Import org. junit. runners. model. InitializationError;
Import org. springframework. test. context. junit4.SpringJUnit4ClassRunner;
Public class ResteasyEmbededServer extends SpringJUnit4ClassRunner {
Private TJWSEmbeddedJaxrsServer server = new TJWSEmbeddedJaxrsServer ();
Public ResteasyEmbededServer (Class <?> Clazz) throws InitializationError {
Super (clazz );
}
@ Override
Public void run (RunNotifier notifier ){
Int port = findAnnotationValueByClass (Port. class). value ();
Class [] resourceClasses = findAnnotationValueByClass (Resources. class). value ();
StartServer (port, resourceClasses );
Try {
Super. run (notifier );
} Finally {
Server. stop ();
}
}
Private void startServer (int port, Class [] resourceClasses ){
Server. setPort (port );
List <Class> actualResourceClasses = server. getDeployment (). getActualResourceClasses ();
Collections. addAll (actualResourceClasses, resourceClasses );
Server. start ();
}
Private <T> T findAnnotationValueByClass (Class <T> annotationClass ){
For (Annotation annotation: getTestClass (). getAnnotations ()){
If (annotation. annotationType (). equals (annotationClass )){
Return (T) annotation;
}
}
Throw new IllegalStateException (String. format ("Can't find % s on test class: % s", annotationClass, getTestClass ()));
}
@ Retention (RetentionPolicy. RUNTIME)
@ Target (ElementType. TYPE)
Public static @ interface Resources {
Public Class [] value ();
}
@ Retention (RetentionPolicy. RUNTIME)
@ Target (ElementType. TYPE)
Public static @ interface Port {
Public int value ();
}
}
TJWSEmbeddedJaxrsServer is an embedded service provided by the RESTEasy framework.
To be continued