如何使用 JAXBContext 和 Marshaller 在 Web 浏览器上显示 XML
How to use JAXBContext and Marshaller to display XML on the web browser
我不想创建文件(我确实知道该怎么做,但我不想创建文件,而是想直接在浏览器中查看结果)
下面的代码试图读取电影列表,使用生成输出 XML 方法并将 xml 发送回我的 get 方法,该方法应在中显示 xml浏览器。
我的 generateOutputXML 方法有效。当我使用 m.marshal(filmList, System.out); 写入控制台时我可以在控制台中看到预期的 XML 但在浏览器中我看到“model.FilmList@10eb45c4”
@WebServlet("/Control")
public class Control extends HttpServlet {
private static final long serialVersionUID = 1L;
public Control() {
super();
}
public static FilmList generateOutputXML (ArrayList<Film> films) throws JAXBException, FileNotFoundException {
// create filmList, assigning film
FilmList filmList = new FilmList();
filmList.setFilmList(films);
StringBuffer outputXML = new StringBuffer();
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(FilmList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
m.marshal(filmList, System.out);
return filmList;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//response.setContentType("text/xml");
PrintWriter out = response.getWriter();
String jsonResult;
Gson gson = new Gson();
FilmDAO fd = new FilmDAO();
ArrayList<Film> filmList = new ArrayList<Film>();
filmList = fd.getAllFilms();
jsonResult = gson.toJson(filmList);
FilmList allFilms = new FilmList();
try {
allFilms = generateOutputXML(filmList);
} catch (FileNotFoundException | JAXBException e) {
e.printStackTrace();
}
//jsonResult = gson.toJson(filmList);
// out.println(jsonResult);
out.println("Test");
out.println(allFilms);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
您正在将生成的 XML 写入 System.out
。没有人可以在 Web 应用程序中看到该输出。它可能 会被记录,但用户看不到日志。
您应该将生成的 XML 直接写入响应编写器。
您似乎还想将响应发送为 JSON。很好,但是您应该让客户指定格式。这通常使用 HTTP header Accept
来完成,但也可以使用查询参数来完成。
这里是示例代码。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Determine content type
String format = request.getParameter("format");
if (format == null || format.equalsIgnoreCase("XML")) {
response.setContentType("text/xml; charset=UTF-8");
} else if (format.equalsIgnoreCase("JSON")) {
response.setContentType("application/json; charset=UTF-8");
} else {
throw new ServletException("Unknown format: \"" + format + "\"");
}
// Get data
FilmDAO fd = new FilmDAO();
ArrayList<Film> films = fd.getAllFilms();
// Render data directly to response stream
try (Writer responseWriter = response.getWriter()) {
if (format == null || format.equalsIgnoreCase("XML")) {
FilmList filmList = new FilmList();
filmList.setFilmList(films);
JAXBContext context = JAXBContext.newInstance(FilmList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(filmList, responseWriter);
} else if (format.equalsIgnoreCase("JSON")) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(films, responseWriter);
}
} catch (JAXBException e) {
throw new ServletException("JAXB failed: " + e, e);
}
}
我不想创建文件(我确实知道该怎么做,但我不想创建文件,而是想直接在浏览器中查看结果)
下面的代码试图读取电影列表,使用生成输出 XML 方法并将 xml 发送回我的 get 方法,该方法应在中显示 xml浏览器。
我的 generateOutputXML 方法有效。当我使用 m.marshal(filmList, System.out); 写入控制台时我可以在控制台中看到预期的 XML 但在浏览器中我看到“model.FilmList@10eb45c4”
@WebServlet("/Control")
public class Control extends HttpServlet {
private static final long serialVersionUID = 1L;
public Control() {
super();
}
public static FilmList generateOutputXML (ArrayList<Film> films) throws JAXBException, FileNotFoundException {
// create filmList, assigning film
FilmList filmList = new FilmList();
filmList.setFilmList(films);
StringBuffer outputXML = new StringBuffer();
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(FilmList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
m.marshal(filmList, System.out);
return filmList;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//response.setContentType("text/xml");
PrintWriter out = response.getWriter();
String jsonResult;
Gson gson = new Gson();
FilmDAO fd = new FilmDAO();
ArrayList<Film> filmList = new ArrayList<Film>();
filmList = fd.getAllFilms();
jsonResult = gson.toJson(filmList);
FilmList allFilms = new FilmList();
try {
allFilms = generateOutputXML(filmList);
} catch (FileNotFoundException | JAXBException e) {
e.printStackTrace();
}
//jsonResult = gson.toJson(filmList);
// out.println(jsonResult);
out.println("Test");
out.println(allFilms);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
您正在将生成的 XML 写入 System.out
。没有人可以在 Web 应用程序中看到该输出。它可能 会被记录,但用户看不到日志。
您应该将生成的 XML 直接写入响应编写器。
您似乎还想将响应发送为 JSON。很好,但是您应该让客户指定格式。这通常使用 HTTP header Accept
来完成,但也可以使用查询参数来完成。
这里是示例代码。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Determine content type
String format = request.getParameter("format");
if (format == null || format.equalsIgnoreCase("XML")) {
response.setContentType("text/xml; charset=UTF-8");
} else if (format.equalsIgnoreCase("JSON")) {
response.setContentType("application/json; charset=UTF-8");
} else {
throw new ServletException("Unknown format: \"" + format + "\"");
}
// Get data
FilmDAO fd = new FilmDAO();
ArrayList<Film> films = fd.getAllFilms();
// Render data directly to response stream
try (Writer responseWriter = response.getWriter()) {
if (format == null || format.equalsIgnoreCase("XML")) {
FilmList filmList = new FilmList();
filmList.setFilmList(films);
JAXBContext context = JAXBContext.newInstance(FilmList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(filmList, responseWriter);
} else if (format.equalsIgnoreCase("JSON")) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(films, responseWriter);
}
} catch (JAXBException e) {
throw new ServletException("JAXB failed: " + e, e);
}
}