webservice+annotation---ONE
来源:互联网 发布:nlpir java 使用 编辑:程序博客网 时间:2024/05/29 16:10
昨天接手一个webservice项目,算是二次开发吧,,看了两天webservice和annotation,记录下我的学习笔记
注解:
@XmlAccessor(XmlAccessType.Field)表示 注解(@XmlElement name="")字段要加在字段上面,如果没有@XmlAccessor注解的话要将@XmlElement注解加在方法上(个人认为:应该是默认是加在getter方法上面)。
@XmlRootElement(name="")生成的xml文件的根元素。
javabean类一定要有个无参构造函数,因为要在字段赋值时用到无参构造函数,否则会报异常:
does not have a no-arg default constructor
嵌套元素,诸如:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<class>
<班级编号>software</班级编号>
</class>
<学号>1001</学号>
<姓名>sun</姓名>
</student>
需要构造一静态内部类,因为内部类不需要访问外围类中的数据,所以设置为静态类(构造内部类对象时不需要外围类的对象),代码如下:
//@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "student")
@XmlType(propOrder = { "classId", "id", "stuName" })
public class Student {
private int id;
private String stuName;
private ClassId classId;
public static class ClassId {
private String className;
public ClassId(String className) {
this.className = className;
}
public ClassId() {
}
@XmlElement(name = "班级编号")
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
public Student(int id, String stuName) {
this.id = id;
this.stuName = stuName;
}
public Student() {
}
@XmlElement(name = "学号")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name = "姓名")
public String getstuName() {
return stuName;
}
public void setstuName(String stuName) {
this.stuName = stuName;
}
@XmlElement(name = "class")
public ClassId getClassId() {
return classId;
}
public void setClassId(ClassId classId) {
this.classId = classId;
}
}
之所以要注释掉@XmlAccessorType,是因为注解是加在getter方法上的,否则会出现
There are two properties named "classId" 异常
因为在生成XML文件时,元素的出现位置是随机的,如果要固定元素的前后位置,就要加入@XmlType(proOrder={})注解。
2、编组List对象,诸如:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<学校>
<school>
<班级>
<班级编号>software</班级编号>
</班级>
<学号>1001</学号>
<姓名>sun</姓名>
</school>
<school>
<班级>
<班级编号>software</班级编号>
</班级>
<学号>1002</学号>
<姓名>klov</姓名>
</school>
</学校>
则需要
//@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "学生")
@XmlType(propOrder = { "classId", "id", "stuName" })
public class Student {
private int id;
private String stuName;
private ClassId classId;
public static class ClassId {
private String className;
public ClassId(String className) {
this.className = className;
}
public ClassId() {
}
@XmlElement(name = "班级编号")
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
public Student(int id, String stuName) {
this.id = id;
this.stuName = stuName;
}
public Student() {
}
@XmlElement(name = "学号")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name = "姓名")
public String getstuName() {
return stuName;
}
public void setstuName(String stuName) {
this.stuName = stuName;
}
@XmlElement(name = "班级")
public ClassId getClassId() {
return classId;
}
public void setClassId(ClassId classId) {
this.classId = classId;
}
}
School类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "学校")
public class School {
// @XmlElement(name = "student")
@XmlElements(value = { @XmlElement(type=Student.class)})
private List<Student> school = new ArrayList<Student>();
public List<Student> getSchool() {
return school;
}
public void setSchool(List<Student> school) {
this.school = school;
}
public synchronized void addStu(Student stu){
if(school == null){
school = new ArrayList<Student>();
}
school.add(stu);
}
}
测试类
@Test
public void testGetId() {
Student stu = new Student(1001, "sun") ;
Student stu2 = new Student(1002, "klov") ;
Student.ClassId classId = new Student.ClassId("software");
stu.setClassId(classId);
stu2.setClassId(classId);
List<Student> stuList = new ArrayList<Student>();
stuList.add(stu);
stuList.add(stu2);
School school = new School();
school.setSchool(stuList);
try {
JAXBContext jc = JAXBContext.newInstance(School.class);
Marshaller marshall = jc.createMarshaller();
marshall.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter writer = new StringWriter();
marshall.marshal(school, writer);
System.out.println(writer);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//不知为何会有<school>标签
原来默认是以private List<Student> school 字段名标识的,而不是以List中标注的根元素名标志的。
--------------如果想拒绝别人的批评,那就只能做个无脑无能无为无知的人--------------------
- webservice+annotation---ONE
- hibernate annotation one-to-one
- JPA -the annotation about one to one
- hibernate annotation one to one 共享主键
- hibernate annotation 双向 one-to-one 注解
- Hibernate – One-To-One Example (Annotation)
- hibernate annotation 双向 one-to-one 注解
- Hibernate – One-to-One example (Annotation)
- Hibernate – One-to-One example (Annotation)
- Hibernate – One-to-One example (Annotation)
- Hibernate – One-to-One example (Annotation)
- hibernate annotation 双向 one-to-one 注解
- hibernate annotation 双向 one-to-one 注解
- webservice 基本Annotation定义
- hibernate annotation one-to-many
- webservice—one
- Hibernate之one-to-one单向外键关联Annotation
- Hibernate之one-to-one双向外键关联Annotation
- PHP 单一入口的特有作用
- 百度收录的数量与流量的关系
- SQL Server2005中的事务和锁(管理大批量并发处理)
- 初步接触 gradle
- 1860
- webservice+annotation---ONE
- android ics SystemUI详解(一)
- UIC 2010-2012 GSM-R项目
- java 常用命令及参数
- linux ipcs确定共享内存使用情况
- 目标关键词优化的具体思路
- 新年新开始
- 输入一个n,在屏幕上打印出N*N的矩阵(华为面试题)
- OpenGL几何图元和缓冲区(二)