[英]Not having a response when I call my web service GET request
I'm a student who needs help with a homework problem.我是一名需要帮助解决家庭作业问题的学生。 The thing is that I wrote a rest web service using SAX parser in order to display a xml file that I have stored on the same folder of the project.问题是我使用 SAX 解析器编写了 rest web 服务,以显示我存储在项目同一文件夹中的 xml 文件。 The problem is that when I use the path I provided to it, it's not happening anything.问题是当我使用我提供给它的路径时,它什么也没发生。 I'm probably doing something wrong on my code.我可能在我的代码上做错了什么。 Here it is:这里是:
package com.crunchify.restjersey;
import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@Path("/saxbooksxml")
public class SaxBooksXml {
public SaxBooksXml(){}
@GET
@Produces(MediaType.APPLICATION_XML)
public void gofindsaxbooks(){
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try {
saxParser = factory.newSAXParser();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DefaultHandler handler = new DefaultHandler(){
boolean bauthor = false;
boolean btitle = false;
boolean bgenre = false;
boolean bprice = false;
boolean bpublish_date = false;
boolean bdescription = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException{
if(qName.equalsIgnoreCase("author")){
bauthor = true;
}
if(qName.equalsIgnoreCase("title")){
btitle = true;
}
if(qName.equalsIgnoreCase("genre")){
bgenre = true;
}
if(qName.equalsIgnoreCase("price")){
bprice = true;
}
if(qName.equalsIgnoreCase("publish_date")){
bpublish_date = true;
}
if(qName.equalsIgnoreCase("description")){
bdescription = true;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException{
}
public void characters(char ch[], int start, int lenght) throws SAXException{
if(bauthor){
System.out.println("author: "+new String(ch, start, lenght));
bauthor = false;
}
if(btitle){
System.out.println("title: "+new String(ch, start, lenght));
btitle = false;
}
if(bgenre){
System.out.println("genre: "+new String(ch, start, lenght));
bgenre = false;
}
if(bprice){
System.out.println("price: "+new String(ch, start, lenght));
bprice = false;
}
if(bpublish_date){
System.out.println("publish_date: "+new String(ch, start, lenght));
bpublish_date = false;
}
if(bdescription){
System.out.println("description: "+new String(ch, start, lenght)+"\n");
bdescription = false;
}
}
};
try {
saxParser.parse("Books.xml", handler);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This is a print of part of the file I'm trying to parse.这是我要解析的文件的一部分的打印。 The idea is to display it on the browser exactly the same.这个想法是在浏览器上完全一样地显示它。
Your method gofindsaxbooks
returns void
您的方法gofindsaxbooks
返回void
Change it like below:更改如下:
@GET
@Produces(MediaType.APPLICATION_XML)
public Response gofindsaxbooks()
{
return Response.status(200).entity("Test XML created").build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.