java - 当我调用 Web 服务 GET 请求时没有响应

标签 java xml rest web-services

我是一名学生,需要帮助解决家庭作业问题。问题是我使用 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 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();
        }
    }
}

这是我试图解析的文件的一部分的打印。我们的想法是在浏览器上显示得一模一样。

enter image description here

最佳答案

您的方法gofindsaxbooks返回void

按如下方式更改:

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

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

关于java - 当我调用 Web 服务 GET 请求时没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58752419/

相关文章:

php - Laravel 7 - 使用 REST API 而不是数据库

java - "Invalid byte 1 of 1-byte UTF-8 sequence"读取 RSS 源时

xml - 如何计算 perl 中元素的 xpath?

rest - 通过 Adob​​e Air 使用 REST API

java - 在没有星期六,星期日和公共(public)假期的情况下计算java中的工作日

xml - 如何使用 Groovy MarkupBuilder 插入 xml 注释?

json - 分页 jsonschema/hyperschema 时保留查询参数

java - Ehcache 2.10 线程被阻塞

java - 在 Java 中对单个字符进行子字符串化

java - 初始化子类字段