0

I am working on an API that will return an JSON response from an XML source. I have used RestTemplate and JAXB to get the XML string from the source and then used StringReader and Unmarshaller to create the Java object. The object looks like this;

@XmlRootElement(name="ItemSearchResponse", namespace="http://webservices.amazon.com/AWSECommerceService/2011-08-01") //
@XmlAccessorType(XmlAccessType.FIELD)
public class SampleXML {

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType
    public static class OperationRequest {
        @XmlTransient
        private String RequestId;

        @XmlElement(name="RequestId")
            public void setRequestId(String id) {
            this.RequestId = id;
        }

        public String getRequestId() {
            return RequestId;
        }
        ...

This is the code that should return the JSON string to the browser;

 @RequestMapping("/samplexml2")
    public SampleXML CreateXMLFile2 () throws EncoderException, FileNotFoundException, SAXException {
         try {

            String requestUrl = null;
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.setErrorHandler(new ResponseErrorHandler());

            String decodedUrl =   "http://localhost:8080/sample.xml";
            String response = restTemplate.getForObject(decodedUrl, String.class);

            //Prepare JAXB objects
             JAXBContext jaxbContext = JAXBContext.newInstance(SampleXML.class);
            Unmarshaller u = jaxbContext.createUnmarshaller();

            //Create an XMLReader to use with our filter
            XMLReader reader = XMLReaderFactory.createXMLReader();

            //Create the filter (to add namespace) and set the xmlReader as its parent.
            NamespaceFilter inFilter = new NamespaceFilter("http://webservices.amazon.com/AWSECommerceService/2011-08-01", true);
            inFilter.setParent(reader);

            //Prepare the input, in this case a java.io.File (output)
            InputSource is = new InputSource(new StringReader(response));

            //Create a SAXSource specifying the filter
            SAXSource source = new SAXSource(inFilter, is);

            //Do unmarshalling
            SampleXML myJaxbObject = (SampleXML) u.unmarshal(source);

            //Convert to myJaxbObject to JSON string here;

            return myJaxbObject;

          } catch (JAXBException e) {
            e.printStackTrace();
            return null;
          }
    }

I want to write the conversation at this line; //Convert to myJaxbObject to JSON string here;

I have read many articles that point to this library; https://github.com/FasterXML/jackson-module-jaxb-annotations But I have not been able to use it successfully.

I would like an example that uses Jackson or MOXy dependencies

  • Possible duplicate of [Converting Java objects to JSON with Jackson](https://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jackson) – Arnaud Sep 19 '17 at 10:03
  • Note that I am using JAXB and I want to use unmarshal. That's why I have shown a small sample of Java Object. –  Sep 19 '17 at 10:05
  • Oops that actually worked. –  Sep 19 '17 at 10:10
  • But I was looking for an example that uses MOXy or Jackson Jackson dependencies –  Sep 19 '17 at 10:15
  • The example provided in the link does use Jackson (ObjectMapper is a part of Jackson) – dsp_user Sep 19 '17 at 10:57

1 Answers1

0

Have you tried simply changing your RequestMapping to @RequestMapping(value = "/samplexml2", produces = MediaType.APPLICATION_JSON_VALUE)?

athom
  • 1,428
  • 4
  • 13
  • 26
  • Can you post the error? I am guessing that you do not have a JSON library like Jackson in your classpath – athom Sep 19 '17 at 10:23
  • I get this in Eclipse `Syntax error on token ""/samplexml2"", invalid MemberValuePairs` –  Sep 19 '17 at 10:31
  • I have updated my answer - you need to add `value =` before your path when adding another property like `produces` – athom Sep 19 '17 at 10:37
  • Thanks that worked! Is that safe without any conversation? How does that work. You might have noticed I am new to Java. I think this is a lot quicker. –  Sep 19 '17 at 10:50
  • By the way, you can probably simplify by doing `SampleXML response = restTemplate.getForObject(decodedUrl, SampleXML.class);` – athom Sep 19 '17 at 11:14
  • Thanks. For so many days now I have been trying to get my SampleXML class to work with RestTemplate. But I thought I had to add some kind of XML converter like `Jaxb2RootElementHttpMessageConverter` but each time the object came back empty. This has solved all my problems. –  Sep 20 '17 at 08:20