XML Support - Quiz
Total: 10 questions
1. Which low level data types does Jersey support?
Which low level data types does Jersey support?
StreamSource, SAXSource, DOMSource and Document.
2. Which XML representations does Jersey support?
Which XML representations does Jersey support?
- Low level (StreamSource, SAXSource, DOMSource and Document);
- JAXB;
- with using JAXBElement.
3. Write the jersey client to test this resource:
@Path("exams")
public class ExamService {
@POST
@Path("StreamSource")
public StreamSource getStreamSource(StreamSource streamSource) {
return streamSource;
}
}
Write the jersey client to test this resource:
@Path("exams")
public class ExamService {
@POST
@Path("StreamSource")
public StreamSource getStreamSource(StreamSource streamSource) {
return streamSource;
}
}Client client = Client.create();
WebResource resource = client.resource(".../exams/StreamSource");
String result = resource.type(MediaType.APPLICATION_XML_TYPE).post(String.class, "<exam/>");
System.out.println(result);
4. Can the resource be accessed by GET .../exam?
@XmlRootElement
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public Exam getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return exam;
}
}
Can the resource be accessed by GET .../exam?
@XmlRootElement
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public Exam getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return exam;
}
}Yes.
5. Write Jersey client to test the resource:
@XmlRootElement
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public Exam getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return exam;
}
}
Write Jersey client to test the resource:
@XmlRootElement
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public Exam getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return exam;
}
}Client client = Client.create();
WebResource resource = client.resource("../exam");
Exam result = resource.accept(MediaType.APPLICATION_XML_TYPE).get(Exam.class);
6. When it is necessary to use JAXBElement class?
When it is necessary to use JAXBElement class?
If there isn't possibility to add JAXB annotations to the source.
7. Is it possible to access the resource by GET .../exam?
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public JAXBElement<Exam> getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return new JAXBElement<Exam>(new QName("exam"), Exam.class, exam);
}
}
Is it possible to access the resource by GET .../exam?
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public JAXBElement<Exam> getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return new JAXBElement<Exam>(new QName("exam"), Exam.class, exam);
}
}Yes.
8. Write Jersey client to test the resource:
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public JAXBElement<Exam> getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return new JAXBElement<Exam>(new QName("exam"), Exam.class, exam);
}
}
Write Jersey client to test the resource:
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@GET
@Produces(MediaType.APPLICATION_XML)
public JAXBElement<Exam> getExam() {
Exam exam = new Exam();
exam.id = 3;
exam.name = "OCEJWSD6";
return new JAXBElement<Exam>(new QName("exam"), Exam.class, exam);
}
}Client client = Client.create();
WebResource resource = client.resource(".../exam");
// GET
GenericType<JAXBElement<Exam>> examType = new GenericType<JAXBElement<Exam>>() {};
Exam exam1 = resource.accept(MediaType.APPLICATION_XML_TYPE).get(examType).getValue();
System.out.println(exam1.name);
System.out.println(exam1.id);
9. Write Jersey client to test the resource:
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@POST
@Consumes(MediaType.APPLICATION_XML)
public void setExam(JAXBElement<Exam> exam) {
System.out.println("setExam " + exam.getValue().name);
System.out.println("setExam " + exam.getValue().id);
}
}
Write Jersey client to test the resource:
public class Exam {
public int id;
public String name;
}
@Path("exam")
public class ExamService {
@POST
@Consumes(MediaType.APPLICATION_XML)
public void setExam(JAXBElement<Exam> exam) {
System.out.println("setExam " + exam.getValue().name);
System.out.println("setExam " + exam.getValue().id);
}
}Client client = Client.create();
WebResource resource = client.resource(".../exam");
// POST
Exam exam = new Exam();
exam.id = 1;
exam.name = "OCJP 6";
resource.post(new JAXBElement<Exam>(new QName("exam"), Exam.class, exam));
10. When it can be necessary to use custom JAXBContext?
When it can be necessary to use custom JAXBContext?
To set some specific things, for example different classloader.