Build an Apache Wink REST service(一)
ApacheWinkserviceconfiguration
ApacheWinkapplicationsaretypicallydeployedinaservletcontainerlikeApacheTomcatandpackagedasaWARfile.LikeanyotherWebapplication,ApacheWinkservicesalsoneedaweb.xmlfile
web.xmlWebconfigurationfileasfollowing:
<web-app> <display-name>Wink demo</display-name> <description>Demonstration of SDK features</description> <!-- Wink SDK servlet configuration. This servlet handles HTTP requests of SDK web service on application server.--> <servlet> <servlet-name>restSdkService</servlet-name> <servlet-class> org.apache.wink.server.internal.servlet.RestServlet </servlet-class> <init-param> <param-name>applicationConfigLocation</param-name> <param-value>/WEB-INF/application</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>restSdkService</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
@WorkspaceAnnotation
Thefollowingexampledemonstratestheuseof@Workspaceannotationontheresourcesinorderto
havetheauto-generatedAPPservicedocumentcontaintheinformationaboutit.
GiventhefollowingcollectionResourcesdefinitions,ResourceAtheresultisdisplayedin
the"AutoGeneratedAPPServiceDocument"tablethatfollows.
@Workspace(workspaceTitle = "Services", collectionTitle = "Service1")
@Path("services/service1")
public class ResourceA {
@POST
@Produces("text/plain")
@Consumes({"application/atom+xml", "application/xml"})
public String getText() {return "hey there1";}
}AutoGeneratedAPPServiceDocument
<service xmlns:atom=http://www.w3.org/2005/Atom
xmlns="http://www.w3.org/2007/app">
<workspace>
<atom:title>Services</atom:title>
<collection href="services/service1">
<atom:title>Service1</atom:title>
<accept>application/xml</accept>
<accept>application/atom+xml</accept>
</collection>
</workspace>
</service>@ScopeAnnotationSpecification
Thefollowingexampleillustrateshowtodefinearesourcewithasingletonlifecycle
@Scope(ScopeType.SINGLETON)
@Path("service1")
public class ResourceA {
...
}somekindsoflifescopesisbelow,youcanchooseonewhichyouneedduringyour
coding
PROTOTYPE,SINGLETON
@ParentAnnotation
The@ParentannotationprovidestheabilitytodefineabasetemplateURIfortheURIspecifiedina
[email protected]@Parentannotation,theApacheWinkruntimecalculatesthefinal
resourcetemplatebyfirstretrievingthevalueofthe@Parentannotation,whichholdstheparentresourceclass,andthenconcatenatestheresourcepathtemplatedefinitiontothepathtemplatedefinitionoftheparentresource.exampleisfollowing:
@Path("services")
public class ParentResource {
...
}
@Parent(ParentResource .class)
@Path("service1")
public class ResourceA {
...
}Intheexample,theuserdefinedtworesources:AParentResourceandResourceA.ParentResource
definesthe@Pathannotationtoassociateitwith"services"URI.ResourceAdefinesthe@Path
annotationtoassociateitwith"service1"URIanddefinesParentResourcetobeitsparentbyspecifying
itinthe@Parentannotation.Inthiscase,thefinalURIpathforResourceAis"services/service1".