here there is a rest server as well as a rest client.same as old ways.the server provides access to the resources and the client makes access to resource as well as can modify the resources.resources are identified by global ids.rest allows resources have different representations.client can get different outputs.(i.e xml,html,json).methods that are supported are get,post,delete,put.
Java defines standard REST support via JAX-RS (The Java API for RESTful Web Services)
@Path("/test")
public class testjersey {
// This method is called if TEXT_PLAIN is request
@GET//the class registers its methods for the get request using @GET annotation.Using the @Produces annotation, it defines that it can deliver several MIME types, text, XML and HTML.
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersaey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersddddey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jeffrsey" + "</title>"
+ "<body><h1>" + "Hello Jerseay" + "</body></h1>" + "</html> ";
}
}
the url to access the webapp is like this( http://localhost:8080/resources/test)
see resources is the servlet mapping in web.xml and test is the name of path(base- uri) in the resource.the browser requests the html representation of your resource.so the output will be: Hello Jerseay
the url to access the webapp is like this( http://localhost:8080/resources/test)
see resources is the servlet mapping in web.xml and test is the name of path(base- uri) in the resource.the browser requests the html representation of your resource.so the output will be: Hello Jerseay
No comments:
Post a Comment