无法显示XML数据

时间:2018-02-09 15:06:51

标签: spring rest jaxb

我需要显示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个IllegalAnnotationExceptions类的计数有两个同名的属性“exerciseList”这个问题是与以下位置相关:at public java.util.List com.rest.model.ExerciseInstructionsList.getExerciseList()    在com.rest.model.ExerciseInstructionsList这个问题与以下位置有关:at 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。

1 个答案:

答案 0 :(得分:0)

当您仔细阅读错误消息时,您会看到原因:(我格式化并突出显示该消息以提高可读性)

  

IllegalAnnotationExceptions类具有同名两个属性&#34; exerciseList &#34;
  此问题与以下位置有关:在com.rest.model.ExerciseInstructionsList的public java.util.List com.rest.model.ExerciseInstructionsList。 getExerciseList ()   此问题与以下位置有关:在com.rest.model.ExerciseInstructionsList的公共java.util.List com.rest.model.ExerciseInstructionsList。 exerciseList

因此,该计划会抱怨您的班级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;
    }
}