
GET, POST, PUT, DELETEhttp://api.fred.com/query/paramimport 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();
}
}

Mort Elvis Einstein
@Entity@Id @GeneratedValue@OneToMany etc.
import javax.persistence.*;
@Entity public class Question implements Serializable {
@Id @GeneratedValue private int id;
private String text;
@OneToMany private Set<Choice> choices;
@OneToOne private Choice answer;
public Question() {} // No-arg constructor is required
...
}
ID, TEXT, ANSWER_ID QUESTION_CHOICE table associates IDs of question and choice
EntityManager em = ...; // see next slide
Query q = em.createQuery("SELECT x FROM Question q WHERE q.id = :id")
.setParameter("id", id);
Question p = (Question) q.getSingleResult(); // Also fetches choices (maybe lazily)
@Stateless
public class UserSB {
@PersistenceContext private EntityManager em;
...
}

path/to/glassfish4/bin/asadmin start-domainhttp://localhost:8080. What happens?mkdir -p lab14/WEB-INF/classesHelloService class into the lab14 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 .:path/to/glassfish4/glassfish/modules/\* *.java. jar cvfM path/to/glassfish4/glassfish/domains/domain1/autodeploy/myapp.war WEB-INFhttp://localhost:8080/myapp/rest/hello/World. What happens?myapp? Why rest? Why hello?World?lab14 directory, and remove the package statement.QuestionService class
import java.util.Scanner;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/question")
public class QuestionService {
@GET
@Path("/{id}")
@Produces("text/plain")
public Response getQuestion(@PathParam("id") int id) {
// Ignoring id for now
Question q = new Question();
q.read(new Scanner("What was the best-selling smartphone OS in 2006?\nAndroid\n*Blackberry\niOS\nWindows Mobile\n"));
return Response.status(200).entity(q.toString()).build();
}
}
http://localhost:8080/myapp/rest/question/1. What happens?Question class:
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Question implements Serializable {
@XmlElement
private String text;
@XmlElement
private List<String> choices = new ArrayList<String>();
@XmlElement
private String answer;
...
}
QuestionService, change text/plain to application/xml and q.toString() to q. Rebuild and point your browser to http://localhost:8080/myapp/rest/question/1. What happens?application/xml to application/json and repeat. What happens?
Question Choice. (We skip Quiz for now)@Entity, @OneToMany, @OneToOne. Remember to add an id field to each class. Also remember that entity classes need a default constructor.path/to/glassfish4/bin/asadmin start-database
QuestionService
AppConfig from the previous steppersistence.xml in WEB-INF/classes/META-INF/persistence.xmlhttp://localhost:8080/myapp/rest/question/initij.properties with this contents:
ij.driver=org.apache.derby.jdbc.ClientDriver ij.protocol=jdbc:derby://localhost:1527/ ij.database=sun-appserv-samplesRun the command
java -jar path/to/glassfish4/javadb/lib/derbyrun.jar ij -p ij.propertiesThen run the commands
select * from question; select * from choice; select * from question_choice;Explain the role of the
question_choice table.
exit; gets you out of ij.http://localhost:8080/myapp/rest/question/129, where the last part is the question id, revealed by the /init command. Make a JPQL query and return the toString of the result.@XmlRootElement, @XmlElement, @Produces)