简体   繁体   中英

Jackson XmlMapper

I have a working method that uses Jackson to post JSON formatted data.

I basically copied that method, changing ObjectMapper to become XmlMapper. I also changed the RequestProperty from application/json to application/xml

   private static void post_xml_to_url(Logger logger, Object infoToPost, URL url, String user, String psw) throws JsonProcessingException, MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException {
    logger.log(Level.INFO, "Posting Ticket..");

    XmlMapper mapper = new XmlMapper(); //Chang back to use JSON
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    byte[] val = mapper.writeValueAsBytes(infoToPost);//Error is thrown here
    System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(infoToPost));
    HttpsURLConnection con = null;
    try {
        disableCertificateValidation();


        con = (HttpsURLConnection) url.openConnection();
        String encoding = Base64.getEncoder().encodeToString((user + ":" + psw).getBytes("UTF-8"));
        con.setRequestProperty("Authorization", String.format("Basic %s", encoding));
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "Java client");

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(val);

        StringBuilder content;
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {

            String line;
            content = new StringBuilder();

            while ((line = in.readLine()) != null) {
                content.append(line);
                content.append(System.lineSeparator());
            }

        }

    } finally {
        con.disconnect();
    }

}

There seems to be some dependency issues

Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.getOutputContext()Lcom/fasterxml/jackson/core/json/JsonWriteContext; at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.close(ToXmlGenerator.java:951) at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3911) at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(ObjectMapper.java:3244)

I'm having a difficult time figuring out which JAR files I need to resolve this. What am I missing?

I have attached a screen shot of the current files in my library:

当前图书馆

The Exception states that com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator lacks the method getOutputContext().

It is possible that you are using the wrong version of this class.

First, determine which jar contains the class. You can do this by running jar -tf on each of the jars in your library, or your IDE may have an easier way to do this.

Once you find the jar containing the class, find the API online for that library, and determine which version is needed to provide the missing method.

I had similar error. I figured out that I had different versions of:

  • jackson-dataformat-xml
  • jackson-annotations

The problem disappeared after updated to similar versions. Use your build package tool to list your library jar versions.

For instance run one of the following commands to get a proper display of your current jar dependencies:

  • mvn dependency:tree or
  • mvn dependency:resolve

The later resolve current maven conflicts too. Hope my answer will help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM