Build an Apache Wink REST service(一)
Apache Wink service configuration
Apache Wink applications are typically deployed in a servlet container like Apache Tomcat and packaged as a WAR file. Like any other Web application, Apache Wink services also need a web.xml file
web.xml Web configuration file as following:
<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>
@Workspace Annotation
The following example demonstrates the use of @Workspace annotation on the resources in order to
have the auto-generated APP service document contain the information about it.
Given the following collection Resources definitions, ResourceA the result is displayed in
the “Auto Generated APP Service Document” table that follows.
@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";} }
Auto Generated APP Service Document
<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>
@Scope Annotation Specification
The following example illustrates how to define a resource with a singleton lifecycle
@Scope(ScopeType.SINGLETON) @Path("service1") public class ResourceA { ... }
some kinds of lifescopes is below ,you can choose one which you need during your
coding
PROTOTYPE, SINGLETON
@Parent Annotation
The @Parent annotation provides the ability to define a base template URI for the URI specified in a
resources @Path annotation.If a resource is annotated with the @Parent annotation, the Apache Wink runtime calculates the final
resource template by first retrieving the value of the @Parent annotation, which holds the parent resource class, and then concatenates the resource path template definition to the path template definition of the parent resource.example is following:
@Path("services") public class ParentResource { ... } @Parent(ParentResource .class) @Path("service1") public class ResourceA { ... }
In the example, the user defined two resources: A ParentResource and ResourceA. ParentResource
defines the @Path annotation to associate it with “services” URI. ResourceA defines the @Path
annotation to associate it with “service1” URI and defines ParentResource to be its parent by specifying
it in the @Parent annotation. In this case, the final URI path for Resour