调用Web服务GET请求时没有响应

我是一名需要家庭作业问题帮助的学生。事实是,我使用SAX解析器编写了一个REST Web服务,以便显示我存储在项目相同文件夹中的xml文件。问题是,当我使用提供给它的路径时,它什么也没有发生。我的代码可能做错了。在这里:

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 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,lenght));
                    btitle = false;
                }
                if(bgenre){
                    System.out.println("genre: "+new String(ch,lenght));
                    bgenre = false;
                }
                if(bprice){
                    System.out.println("price: "+new String(ch,lenght));
                    bprice = false;
                }
                if(bpublish_date){
                    System.out.println("publish_date: "+new String(ch,lenght));
                    bpublish_date = false;
                }
                if(bdescription){
                    System.out.println("description: "+new String(ch,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();
        }
    }
}

这是我要解析的文件的一部分。想法是在浏览器上完全一样地显示它。

enter image description here

myslippers 回答:调用Web服务GET请求时没有响应

您的方法gofindsaxbooks返回void

按如下所示进行更改:

        @GET
        @Produces(MediaType.APPLICATION_XML)
        public Response gofindsaxbooks()
        {

            return Response.status(200).entity("Test XML created").build();
        }
本文链接:https://www.f2er.com/3143400.html

大家都在问