【问题标题】:Unable to show XML data无法显示 XML 数据
【发布时间】:2018-02-09 15:06:51
【问题描述】:

您好,我需要同时显示 xml 和 json 数据。 我可以通过 JaxB 在本地看到这一点,但无法在服务器中看到相同的代码。 每当我将其部署到服务器时,都会出现此错误。 我不知道如何解决这个错误。 无法解决这个问题,尝试了很多,但什么都没有发生,在本地一切都很好,但是当涉及到服务器时,它会显示不同的异常。

错误 500:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.http.converter.HttpMessageConversionException:无法为类 [class com.rest.model.ExerciseInstructionsList] 实例化 JAXBContext:null;嵌套异常是 javax.xml.bind.JAXBException - 带有链接异常:[com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 类有两个同名的属性“exerciseList” 这个问题是与以下位置相关:在 public java.util.List com.rest.model.ExerciseInstructionsList.getExerciseList() 在 com.rest.model.ExerciseInstructionsList 这个问题与以下位置有关:在 public java.util.List com.rest.model.ExerciseInstructionsList.exerciseList at com.rest.model.ExerciseInstructionsList]

我的控制器是

@Controller
@RequestMapping("/")
public class ExerciseController {

    @Autowired
    private ExerciseService exerciseService;


    private static final Logger logger = LoggerFactory.getLogger(ExerciseController.class);

    @Consumes
    @Produces
    @RequestMapping(value=OaesRestURIConstants.GET_EXERCISE_ALL,method=RequestMethod.GET,produces={"application/json"})
    public @ResponseBody List<ExerciseInstructions> getAllExercise()throws Exception{


        logger.info("Start getAllExercises.");

        System.out.println("<<<<<<<<<<<<<<<<<--------------Coming Inside List Exercise Controller----------->>>>>>>>>>>");
        List<ExerciseInstructions> listExercise = new ArrayList<ExerciseInstructions>();
        //ExerciseInstructionsList exe = new ExerciseInstructionsList();


        /*This list contains Exercise Instructions Data*/
        listExercise = exerciseService.getAllExercise();

        /*here i kept the list in     ExerciseInstructionsList    list so that i can fetch xml data also and can show the list.*/
        //exe.setExerciseList(listExercise);

        return  listExercise;
    }



    @RequestMapping(value=OaesRestURIConstants.GET_EXERCISE_XML_ALL,method=RequestMethod.GET,produces={"application/xml"})
    public @ResponseBody ExerciseInstructionsList getAllXmlExercise()throws Exception{


        logger.info("Start getAllExercises.");

        System.out.println("<<<<<<<<<<<<<<<<<--------------Coming Inside List Exercise Controller----------->>>>>>>>>>>");
        List<ExerciseInstructions> listExercise = new ArrayList<ExerciseInstructions>();
        ExerciseInstructionsList exeList = new ExerciseInstructionsList();


        /*This list contains Exercise Instructions Data*/
        listExercise = exerciseService.getAllExercise();

        /*here i kept the list in     ExerciseInstructionsList    list so that i can fetch xml data also and can show the list.*/
        exeList.setExerciseList(listExercise);

        return  exeList;
    }

    @RequestMapping(value=OaesRestURIConstants.EXERCISE_SAVE,method=RequestMethod.POST)
    public @ResponseBody ExerciseInstructions saveExercise(@RequestBody ExerciseInstructions exerciseInstructions)throws Exception{

        logger.info("Start saveExercise.");
        exerciseService.saveExercise(exerciseInstructions);


        return exerciseInstructions;
    }



//@Consumes({"application/xml","application/json"})
   // @Produces({"application/xml","application/json"})
    @RequestMapping(value=OaesRestURIConstants.GET_EXERCISE_ID,method=RequestMethod.GET,produces={"application/xml","application/json"})
    public @ResponseBody ExerciseInstructions getExerciseById(@PathVariable("id") String exerciseId ) throws Exception{

        logger.info("Start getExerciseById. ID="+exerciseId);

        ExerciseInstructions exercise = null;



         try {
             exercise = exerciseService.getExerciseById(exerciseId);

              } catch (Exception e) {
               e.printStackTrace();
              }
        System.out.println("Coming Here>>>>>>>>>>>"+exercise);
        return exercise;
    //return exerciseService.getExerciseById(exerciseId);

    }


    @RequestMapping(value=OaesRestURIConstants.EXERCISE_DELETE,method=RequestMethod.PUT)
    public @ResponseBody ExerciseInstructions deleteById(@PathVariable("id") String exerciseId) throws Exception{

        logger.info("Start deleteExercise.");
        exerciseService.deleteExercise(exerciseId);

        return null;
    }

}

我的模型类是:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ExerciseInstructions {}

我的模型列表类是:

@XmlRootElement(name="exerciseInstructions")
//@XmlSeeAlso({ExerciseInstructions.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class ExerciseInstructionsList {

    public List<ExerciseInstructions> exerciseList;

    public List<ExerciseInstructions> getExerciseList() {
        return exerciseList;
    }

    public void setExerciseList(List<ExerciseInstructions> exerciseList) {
        this.exerciseList = exerciseList;
    }


}

所以任何人都可以帮助我。
我想获取并查看 xml 和 json。

【问题讨论】:

    标签: spring rest jaxb


    【解决方案1】:

    当您仔细阅读错误消息时,您会发现原因:(我对消息进行了格式化并突出显示以提高可读性)

    IllegalAnnotationExceptions 类具有两个同名“exerciseList”的属性
    此问题与以下位置有关:在 public java.util.List com.rest.model.ExerciseInstructionsList.getExerciseList() at com.rest.model.ExerciseInstructionsList
    此问题与以下位置有关:在 public java.util.List com.rest.model.ExerciseInstructionsList.exerciseList 在 com.rest.model.ExerciseInstructionsList

    所以程序抱怨你的类ExerciseInstructionsList有两个属性可以映射到exerciseList,它们是getExerciseList()和exerciseList。

    要解决此问题,您可以将 exerciseList 声明为 private。

    @XmlRootElement(name="exerciseInstructions")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class ExerciseInstructionsList {
        private List<ExerciseInstructions> exerciseList;
    
        public List<ExerciseInstructions> getExerciseList() {
            return exerciseList;
        }
    
        public void setExerciseList(List<ExerciseInstructions> exerciseList) {
            this.exerciseList = exerciseList;
        }
    }
    

    【讨论】:

    • @manaskumar 请花时间阅读What should I do when someone answers my question?。 接受最能解决您问题的答案,不要添加评论说“谢谢”
    • 好的,我工作很忙,所以忘记阅读条款和条件....感谢您的帮助。它也正常工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2011-09-09
    • 2013-05-07
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多