Não ter uma resposta quando eu chamo o meu pedido GET serviço web

user12228689:

Eu sou um estudante que precisa de ajuda com um problema de lição de casa. A coisa é que eu escrevi um serviço web resto usando parser SAX, a fim de exibir um arquivo xml que eu tenha armazenado na mesma pasta do projeto. O problema é que quando eu uso o caminho que eu forneci a ele, isso não está acontecendo nada. Eu provavelmente estou fazendo algo errado no meu código. Aqui está:

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();
        }
    }
}

Esta é uma cópia de parte do arquivo que estou tentando analisar. A ideia é exibi-lo no navegador exatamente o mesmo.

digite descrição da imagem aqui

Mike Adamenko:

Seu método de gofindsaxbooksretornosvoid

Mudá-lo como abaixo:

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

            return Response.status(200).entity("Test XML created").build();
        }

Acho que você gosta

Origin http://43.154.161.224:23101/article/api/json?id=362505&siteId=1
Recomendado
Clasificación