gpt4 book ai didi

java - 将参数传递给 REST Web 服务

转载 作者:行者123 更新时间:2023-12-02 09:36:51 25 4
gpt4 key购买 nike

我正在处理将参数传递给网络服务的问题。

我已经创建了网络服务,对于 fromLanguage = "eng"的情况可以正常工作

但是,当我通过 Glassfish 控制台测试服务并发送 fromLanguage = "bos"时,我没有得到适当的结果。

package pckgTranslator;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/MyRestService/{wordToTranslate},{fromLanguage},{toLanguage}")
public class clsTranslate {
@GET
public String doGet(@PathParam("wordToTranslate") String wordToTranslate,
@PathParam("fromLanguage") String fromLanguage, @PathParam("toLanguage") String toLanguage)
throws Exception{
Translator translator = new Translator();
return translator.getTranslation(wordToTranslate,fromLanguage, toLanguage);
}

}

这是我尝试解析的 XML fajl:

<?xml version="1.0" encoding="utf-8" ?>
<gloss>
<word id="001">
<eng>ball</eng>
<bos>lopta</bos>
</word>
<word id="002">
<eng>house</eng>
<bos>kuca</bos>
</word>
<word id="003">
<eng>game</eng>
<bos>igra</bos>
</word>
</gloss>

这是我用来解析 XML 的类。

package pckgTranslator;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Translator {

String translation = null;

String getTranslation(String wordForTransl, String fromLanguage, String toLanguage)
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

//fromLanguage = "eng";
//toLanguage = "bos";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = Translator.class.getResourceAsStream("/resource/glossary.xml");
Document doc = builder.parse(new InputSource(is));

XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();

//XPathExpression expr = null; //xpath.compile("//word[eng='house']/bos/text()");
XPathExpression expr = xpath.compile("//word['" + wordForTransl + "'='" + wordForTransl + "']/bos/text()");
if (fromLanguage == "eng") {
expr = xpath.compile("//word[eng='" + wordForTransl + "']/bos/text()");
} else if (fromLanguage == "bos") {
expr = xpath.compile("//word[bos='" + wordForTransl + "']/eng/text()");

}

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
//System.out.println(nodes.item(i).getNodeValue());
translation = nodes.item(i).getNodeValue();
}
//return nodes.item(i).getNodeValue();
if (translation != null) {
return translation;
} else {
return "We are sorry, there is no translation for this word!";
}
}
}

在我看来,fromLanguage 和 toLanguage 参数有问题,但我不知道到底是什么问题。提前致谢。

最佳答案

正如我在评论中提到的,您已硬编码 fromLanguagetoLanguage变量为engbos getTranslation()开头方法。因此,fromLanguage和'toLangugae values passed to getTranslation()` 方法丢失。

其次,不要分隔@PathParm通过,/分隔它们。它看起来像:

@Path("/MyRestService/{wordToTranslate}/{fromLanguage}/{toLanguage}")
@GET
public String doGet(@PathParam("wordToTranslate") String wordToTranslate,
@PathParam("fromLanguage") String fromLanguage, @PathParam("toLanguage") String toLanguage) throws Exception

Invocation: curl -X GET http://localhost:8080/MyRestService/x/y/z

或者使用@QueryParam 。在这种情况下,您的路径将是:

@Path("/MyRestService")
public String doGet(@QueryParam("wordToTranslate") String wordToTranslate,
@QueryParam("fromLanguage") String fromLanguage, @QueryParam("toLanguage") String toLanguage) throws Exception

Invocation: curl -X GET http://localhost:8080/MyRestService?wordToTranslate=x&fromLanguage=y&toLanguage=z

删除或注释 getTranslation() 中的以下行方法:

fromLanguage = "eng";
toLanguage = "bos";
<小时/>

注意: 要解决您的问题,上述解决方案就足够了。但是,为了让您更好地编码,请参阅以下建议。除了上述问题之外,我还发现两个问题:

  • 您正在将翻译后的值存储在 translation 中实例变量。如果您使用相同的 Translator 对象(单个实例)并且当前翻译失败,getTranslation()将返回之前翻译的值。
  • 为什么要初始化expr与下面的?
   XPathExpression expr = xpath.compile("//word['" + wordForTransl + "'='" + wordForTransl + "']/bos/text()");
  • 最后,每次您调用getTranslation()时正在解析 XML。相反,在 init() 中解析一次方法,然后在 getTranslation() 中使用它方法。

我修改了你的Translator基于以上几点的类:

package org.openapex.samples.misc.parse.xml;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.io.IOException;
import java.io.InputStream;

public class ParseXMLAndTranslate {
public static void main(String[] args) throws Exception{
Translator translator = new Translator();
translator.init();
System.out.println(translator.getTranslation("house","eng", "bos"));
System.out.println(translator.getTranslation("igra","bos", "eng"));
}

private static class Translator {
//String translation = null;
private Document doc;
public void init() throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = Translator.class.getResourceAsStream("/resource/glossary.xml");
this.doc = builder.parse(new InputSource(is));
}

String getTranslation(String wordForTransl, String fromLanguage, String toLanguage)
throws XPathExpressionException {
//fromLanguage = "eng";
//toLanguage = "bos";
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();

//XPathExpression expr = null; //xpath.compile("//word[eng='house']/bos/text()");
//XPathExpression expr = xpath.compile("//word['" + wordForTransl + "'='" + wordForTransl + "']/bos/text()");
XPathExpression expr = null;
if (fromLanguage == "eng") {
expr = xpath.compile("//word[eng='" + wordForTransl + "']/bos/text()");
} else if (fromLanguage == "bos") {
expr = xpath.compile("//word[bos='" + wordForTransl + "']/eng/text()");
}

Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
String translation = null;
/*for (int i = 0; i < nodes.getLength(); i++) {
//System.out.println(nodes.item(i).getNodeValue());
translation = nodes.item(i).getNodeValue();
}*/
if(nodes.getLength() > 0){
translation = nodes.item(0).getNodeValue();
}
//return nodes.item(i).getNodeValue();
if (translation != null) {
return translation;
} else {
return "We are sorry, there is no translation for this word!";
}
}
}
}

这是输出:

kuca
game

关于java - 将参数传递给 REST Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com