com.sun.istack.SAXException2: class java.util.LinkedHashMap nor any of its super class is known to this context

浪子不回头ぞ 提交于 2019-12-06 10:46:57

If I understand your environment set up, you are using Jackson-JAXB framework to marshal and unmarshal data-objects. Please correct me if I am wrong.

Try below these 2 options first separately and then perhaps together:

  1. Change the type of input parameter in handlePostRequest method to String:

public String handlePostRequest(String resource) {

System.out.println("\nINSIDE HANDLE POST: " + resource);
}

Please let me know if it runs and what it prints. I suspect framework is trying to map object type to LinkedHashMap which is default mapping.

  1. Modify the annotation tags with following:

    @POST

    @Produces({"application/xml", "application/json"})
    
    @Consumes({"application/xml", "application/json"})
    

The issue got resolved after I used jackson library in client too for converting POJO to json rather than JAXB.

Client code now:

 ContentInstance cont = new ContentInstance();
 cont.setContent("Sid");
 cont.setStateTag(BigInteger.ONE);
 cont.setContentSize(BigInteger.TEN);

 ObjectMapper om = new ObjectMapper();
 om.setPropertyNamingStrategy(new CustomNamingStrategy());

        try {
            System.out.print("OBJECT MAPPER:---> JSON STRING" + om.writerWithDefaultPrettyPrinter().writeValueAsString(cont));
            jsonContent = om.writerWithDefaultPrettyPrinter().writeValueAsString(cont);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

The issue was my content parameter in my POJO was of type Object and JAXB was making a LinkedHashMap after converting to json, which was not provided in the JAXB context (thus the error). Now, using ObjectMapper, content is corrrectly converted to Json.

Ex: Earlier using JAXB content was converted to a LinkedaHashMap

content: {"type":"String", "value":"Sid"}

Using Jackson ObjectMapper:

{content: "Sid"}

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