获取javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“Students”)

时间:2015-10-10 01:57:43

标签: java xml web-services jersey

我正在尝试获取一个将通过Jersey生成XML响应的Web服务,并将数据放在散列图中。

我收到以下错误消息:

  

引起:javax.ws.rs.WebApplicationException:javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“students”)。预期的要素是   < {}学生>,< {}学生列表>

我在尝试从hashmap生成XML响应时遇到了一些严重的问题,所以我试图将hashmap转换为数组列表并将其用于XML响应

@GET
@Path("/student")
@Produces({MediaType.APPLICATION_XML,"text/xml"})//changed from text/xml
public Response readStudents(){
    final List<Student>sl = studentList.getStudentListAll();

    final GenericEntity<List<Student>>entity = new GenericEntity<List<Student>>(sl){};

    return Response.ok(entity).build();
}

学生班:

@XmlRootElement //(name = "student")
public class Student {
private String name;
private String grade; 


@XmlElement(name="name")
public String getName(){return name;}
public void setName(String name){
    this.name = name; 
}

@XmlElement(name="grade")
public String getGrade(){return grade;}
public void setGrade(String grade){
    this.grade = grade;
}

最后,StudentList类:

@XmlRootElement (namespace = "myname")
public class StudentList {

//generate wrapper element around students
@XmlElementWrapper(name="student-list")
//set the name of the entities
@XmlElement(name="student")
private ArrayList<Student> studentArrayL = new ArrayList<Student>();

private Map <String, Student> studentList = new ConcurrentHashMap<String, Student>();



//sets the student list 
public void setStudentList(String name, String grade){

    Student student = new Student();

    //if ((name != null && !name.isEmpty()) && (grade !=null && !grade.isEmpty())){
        student.setName(name);
        student.setGrade(grade);
        this.studentList.put(name, student);    

        ArrayList<Student> sal = new ArrayList<Student>(studentList.values());
        studentArrayL = sal;
    //}
}



public ArrayList<Student> getStudentListAll(){return studentArrayL;}

public Map<String, Student> getStudentList(){return studentList;}

public void removeStudent(String key){
    studentList.remove(key); 
}

}

我需要运行glassfish 3.1和1.X版本的球衣。如果有办法从hashmap创建XML响应,我宁愿这样做,但通过我的研究,看起来最好的方法是转换为arraylist。

0 个答案:

没有答案