com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class

橙三吉。 提交于 2019-12-11 12:36:47

问题


I am trying to make a web service dictionary that would parse an XML file containing words in two different languages (Serbian and Italian) and return the translation to a client. The dictionary.xml is placed in the project root folder and looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<dictionary>
    <word>
        <sr>automobil</sr>
        <it>macchina</it>
    </word>
    <word>
        <sr>cvet</sr>
        <it>fiore</it>
    </word>
    <word>
        <sr>knjiga</sr>
        <it>libro</it>
    </word>
    <word>
        <sr>pas</sr>
        <it>cane</it>
    </word>
    <word>
        <sr>jabuka</sr>
        <it>mela</it>
    </word>
</dictionary>

I created an interface that has only one WebMethod that receives a word, original language and the destination laguage:

package service;

@WebService
public interface Translator {

    @WebMethod
    String translate(String original, String orgLang, String destLang);   
}

And I wrote an implementation class that looks like this:

    @WebService(endpointInterface = "service.Translator")
    public class TranslatorClass implements Translator {

        Document doc;
        String translation;
        Element root;

        public TranslatorClass() throws ParserConfigurationException, SAXException, IOException {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder.parse(new File("dictionary.xml"));
            root = doc.getDocumentElement();
        }

        @Override
        public String translate(String original, String orgLang, String destLang) {

            if(orgLang.equals("sr")) {
                NodeList srNodeList = root.getElementsByTagName("sr");
                for(int i=0; i<srNodeList.getLength(); i++) {
                    Node node = srNodeList.item(i);
                    if(node.getTextContent().equals(original)) {
                        translation = node.getNextSibling().getTextContent();
                     }
                }
            }
            else {
                NodeList itNodeList = root.getElementsByTagName("it");
                for(int i=0; i<itNodeList.getLength(); i++) {
                    Node node = itNodeList.item(i);
                    if(node.getTextContent().equals(original)) {
                        translation = node.getPreviousSibling().getTextContent();
                     }
                }
            }

            return translation;
    }
 }

When I try to test it on the GlassFish server, I get the error from the title of the post. Can anyone give me a hand with this one, please? Should the xml file be placed in some other location?

Thanks.


回答1:


OK, I found the answer to this. I moved the logic for reading the file in the web method "translate". The builder couldn't find the file it needed to parse with this version of code anyway. Finally, I solved the problem by placing the xml file in the same package as the TranslatorClass and calling InputStream stream = TranslatorClass.class.getResourceAsStream("dictionary.xml"); After that, I just passed the stream to the builder to parse and it worked.



来源:https://stackoverflow.com/questions/34345732/com-sun-enterprise-container-common-spi-util-injectionexception-error-creating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!