No quiz today
GET
, POST
, PUT
, DELETE
http://api.fred.com/query/param
import javax.ws.rs.*; import javax.ws.rs.core.*; @Path("/hello") public class HelloService { @GET @Path("/{name}") public Response getText(@PathParam("name") String name) { String output = "Hello " + name + "\n"; return Response.status(200).entity(output).build(); } }
import javax.xml.bind.annotation.*; @XmlRootElement public class Person { private String name; public String getName() { return name; } public void setName(String newValue) { name = newValue; } }
<name>Fred</name>
or {"name": "Fred" }
curl https://api.github.com/users/cayhorstmann/events
. What happens?curl -i https://api.github.com/users/cayhorstmann/events
. What is the content type?~/glassfish-3.1.2.2/glassfish/bin/asadmin start-domain
http://localhost:8080
. What happens?workspace
directory, run mkdir -p lab16/WEB-INF/classes
HelloService
class into the workspace/lab16
directoryimport java.util.*; import javax.ws.rs.*; import javax.ws.rs.core.*; @ApplicationPath("/rest") public class AppConfig extends Application { public Set<Class<?>> getClasses() { return new java.util.HashSet<Class<?>>(Arrays.asList(HelloService.class)); } }
javac -d WEB-INF/classes -cp .:~/glassfish-3.1.2.2/glassfish/modules/\* *.java
jar cvfM ~/glassfish-3.1.2.2/glassfish/domains/domain1/autodeploy/greeting.war WEB-INF/classes/*
http://localhost:8080/greeting/rest/hello/World
. What happens?greeting
? Why rest
? Why hello
?World
?@Produces("text/plain")
to the getText
message, and add the following method:
@GET @Path("/{name}") @Produces("application/xml") public Response getXML(@PathParam("name") String name) { String output = "<hello>" + name + "</hello>\n"; return Response.status(200).entity(output).build(); }Recompile and redeploy. Run
curl -H "Accept: text/plain" localhost:8080/greeting/rest/hello/World curl -H "Accept: application/xml" localhost:8080/greeting/rest/hello/WorldWhat happens?
text/html
. What happens?<body><h1>Hello</h1><h2>...</h2></body>
. What did you do?curl
for text/html
?localhost:8080/greeting/rest/hello/World
?Person
class to the lab16
directory. Also add this class:
import javax.xml.bind.annotation.*; @XmlRootElement public class Greeting { private String message; private Person greeted; public String getMessage() { return message; } public void setMessage(String newValue) { message = newValue; } public Person getGreeted() { return greeted; } public void setGreeted(Person newValue) { greeted = newValue; } }
getXML
method, change output
to an object of type Greeting
. Make a Person
, set its name
, set it as the greeted
one, and set message
to "Hello"
. Compile and deploy.curl -H "Accept: application/xml" localhost:8080/greeting/rest/hello/World
?,application/json
to the @Produces
annotation. Compile and deploy. set the header to Accept: application/json
. What happens?