RESTful web services
A RESTful web accommodations are predicated on HTTP methods and the concept of REST. A RESTFul web service defines the base URI for the accommodations, the fortified MIME-types and operations like POST, GET, etc.
JAX-RS annotations
@PATH(your_path): Sets the path to base URL + /defined_path. The base URL is based on the application name (Web Project name), the servlet and the URL pattern from the web.xml configuration file.
@POST : Denotes that the following method will answer to an HTTP POST request.
@GET : Denotes that the following method will answer to an HTTP GET request.
@PUT : Denotes that the following method will answer to an HTTP PUT request.
@DELETE : Denotes that the following method will answer to an HTTP DELETE request.
@Produces(MediaType.TEXT_PLAIN) :@Produces defines the MIME type that would be produce by the method annotated with @GET. In this case, text/plain is the mime type.
@Consumes(MediaType.TEXT_PLAIN) : @Consumes defines which MIME type is consumed by the method.
Example:
index.html
<form id="frmReg" action="http://localhost:8080/FileUploadServlet/mobile/service/formSubmit" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="submit" value="submit" />
</form>
Democontroller.java
@Path("/service")
public class Democontroller {
@POST
@Path("/formSubmit")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public String formSubmit(@FormDataParam("name") String name){
System.out.println("*****************"+name);
return "1";
}
}
AppServlet.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
public class AppServlet extends DispatcherServlet {
private static final long serialVersionUID = 1L;
protected void doDispatch(HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
super.doDispatch(request, response);
}
}
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app-context-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>AppServlet</servlet-name>
<servlet-class>com.evon.servlet.AppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AppServlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.evon.controller</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST Service</servlet-name>
<url-pattern>/mobile/*</url-pattern>
</servlet-mapping>
0 Comment(s)