总结
互联网大厂比较喜欢的人才特点:对技术有热情,强硬的技术基础实力;主动,善于团队协作,善于总结思考。无论是哪家公司,都很重视高并发高可用技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。
**另外本人还整理收藏了2021年多家公司面试知识点以及各种技术点整理 **
下面有部分截图希望能对大家有所帮助。
- @XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。
三、示例
1、准备工作
-
package utils;
-
import java.io.StringReader;
-
import java.io.StringWriter;
-
import javax.xml.bind.JAXBContext;
-
import javax.xml.bind.Marshaller;
-
import javax.xml.bind.Unmarshaller;
-
/**
-
* Jaxb2工具类
-
* @author zhuc
-
* @create 2013-3-29 下午2:40:14
-
*/
-
public class JaxbUtil {
-
/**
-
* JavaBean转换成xml
-
* 默认编码UTF-8
-
* @param obj
-
* @param writer
-
* @return
-
*/
-
public static String convertToXml(Object obj) {
-
return convertToXml(obj, “UTF-8”);
-
}
-
/**
-
* JavaBean转换成xml
-
* @param obj
-
* @param encoding
-
* @return
-
*/
-
public static String convertToXml(Object obj, String encoding) {
-
String result = null;
-
try {
-
JAXBContext context = JAXBContext.newInstance(obj.getClass());
-
Marshaller marshaller = context.createMarshaller();
-
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
-
StringWriter writer = new StringWriter();
-
marshaller.marshal(obj, writer);
-
result = writer.toString();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return result;
-
}
-
/**
-
* xml转换成JavaBean
-
* @param xml
-
* @param c
-
* @return
-
*/
-
@SuppressWarnings(“unchecked”)
-
public static T converyToJavaBean(String xml, Class c) {
-
T t = null;
-
try {
-
JAXBContext context = JAXBContext.newInstance©;
-
Unmarshaller unmarshaller = context.createUnmarshaller();
-
t = (T) unmarshaller.unmarshal(new StringReader(xml));
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return t;
-
}
-
}
非常简单易懂,需要注意的是
-
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
Marshaller.JAXB_FORMATTED_OUTPUT 决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml)
Marshaller.JAXB_ENCODING xml的编码方式
另外,Marshaller 还有其他Property可以设置,可以去查阅api。
2、最简单转换
-
package t1;
-
import java.util.Date;
-
import javax.xml.bind.annotation.XmlAccessType;
-
import javax.xml.bind.annotation.XmlAccessorType;
-
import javax.xml.bind.annotation.XmlAttribute;
-
import javax.xml.bind.annotation.XmlElement;
-
import javax.xml.bind.annotation.XmlRootElement;
-
import javax.xml.bind.annotation.XmlType;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:49:48
-
*/
-
@XmlAccessorType(XmlAccessType.FIELD)
-
@XmlRootElement
-
@XmlType(name = “book”, propOrder = { “author”, “calendar”, “price”, “id” })
-
public class Book {
-
@XmlElement(required = true)
-
private String author;
-
@XmlElement(name = “price_1”, required = true)
-
private float price;
-
@XmlElement
-
private Date calendar;
-
@XmlAttribute
-
private Integer id;
-
/**
-
* @return the author
-
*/
-
public String getAuthor() {
-
return author;
-
}
-
/**
-
* @return the price
-
*/
-
public float getPrice() {
-
return price;
-
}
-
/**
-
* @return the calendar
-
*/
-
public Date getCalendar() {
-
return calendar;
-
}
-
/**
-
* @return the id
-
*/
-
public Integer getId() {
-
return id;
-
}
-
/**
-
* @param author the author to set
-
*/
-
public void setAuthor(String author) {
-
this.author = author;
-
}
-
/**
-
* @param price the price to set
-
*/
-
public void setPrice(float price) {
-
this.price = price;
-
}
-
/**
-
* @param calendar the calendar to set
-
*/
-
public void setCalendar(Date calendar) {
-
this.calendar = calendar;
-
}
-
/**
-
* @param id the id to set
-
*/
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
/* (non-Javadoc)
-
* @see java.lang.Object#toString()
-
*/
-
@Override
-
public String toString() {
-
return “Book [author=” + author + “, price=” + price + “, calendar=” + calendar + “, id=” + id + “]”;
-
}
-
}
-
package t1;
-
import java.util.Date;
-
import javax.xml.bind.JAXBException;
-
import org.junit.Test;
-
import utils.JaxbUtil;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:50:00
-
*/
-
public class JaxbTest1 {
-
/**
-
* @throws JAXBException
-
*/
-
@Test
-
public void showMarshaller() {
-
Book book = new Book();
-
book.setId(100);
-
book.setAuthor(“James”);
-
book.setCalendar(new Date());
-
book.setPrice(23.45f); //默认是0.0
-
String str = JaxbUtil.convertToXml(book);
-
System.out.println(str);
-
}
-
/**
-
* @throws JAXBException
-
*/
-
@Test
-
public void showUnMarshaller() {
-
String str = “<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>” +
-
“<book id=\“100\”>” +
-
" James" +
-
" 2013-03-29T09:25:56.004+08:00" +
-
" <price_1>23.45</price_1>" +
-
“”;
-
Book book = JaxbUtil.converyToJavaBean(str, Book.class);
-
System.out.println(book);
-
}
-
}
输出结果分别为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>< book id=“100”>
James
2013-03-29T14:50:58.974+08:00
<price_1>23.45</price_1>
< /book>
Book [author=James, price=23.45, calendar=Fri Mar 29 09:25:56 CST 2013, id=100]
3、类中包含复杂对象的转换
-
package t2;
-
import javax.xml.bind.annotation.XmlAccessType;
-
import javax.xml.bind.annotation.XmlAccessorType;
-
import javax.xml.bind.annotation.XmlAttribute;
-
import javax.xml.bind.annotation.XmlElement;
-
import javax.xml.bind.annotation.XmlRootElement;
-
import javax.xml.bind.annotation.XmlType;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:51:44
-
*/
-
@XmlAccessorType(XmlAccessType.FIELD)
-
@XmlRootElement(name = “student”)
-
@XmlType(propOrder = {})
-
public class Student {
-
@XmlAttribute
-
private Integer id;
-
@XmlElement
-
private String name;
-
@XmlElement(name = “role”)
-
private Role role;
-
/**
-
* @return the id
-
*/
-
public Integer getId() {
-
return id;
-
}
-
/**
-
* @return the name
-
*/
-
public String getName() {
-
return name;
-
}
-
/**
-
* @return the role
-
*/
-
public Role getRole() {
-
return role;
-
}
-
/**
-
* @param id the id to set
-
*/
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
/**
-
* @param name the name to set
-
*/
-
public void setName(String name) {
-
this.name = name;
-
}
-
/**
-
* @param role the role to set
-
*/
-
public void setRole(Role role) {
-
this.role = role;
-
}
-
/* (non-Javadoc)
-
* @see java.lang.Object#toString()
-
*/
-
@Override
-
public String toString() {
-
return “Student [id=” + id + “, name=” + name + “, role=” + role + “]”;
-
}
-
}
-
package t2;
-
import javax.xml.bind.annotation.XmlAccessType;
-
import javax.xml.bind.annotation.XmlAccessorType;
-
import javax.xml.bind.annotation.XmlElement;
-
import javax.xml.bind.annotation.XmlType;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:51:52
-
*/
-
@XmlAccessorType(XmlAccessType.FIELD)
-
@XmlType(propOrder = { “name”, “desc” })
-
public class Role {
-
@XmlElement
-
private String name;
-
@XmlElement
-
private String desc;
-
/**
-
* @return the name
-
*/
-
public String getName() {
-
return name;
-
}
-
/**
-
* @return the desc
-
*/
-
public String getDesc() {
-
return desc;
-
}
-
/**
-
* @param name the name to set
-
*/
-
public void setName(String name) {
-
this.name = name;
-
}
-
/**
-
* @param desc the desc to set
-
*/
-
public void setDesc(String desc) {
-
this.desc = desc;
-
}
-
/* (non-Javadoc)
-
* @see java.lang.Object#toString()
-
*/
-
@Override
-
public String toString() {
-
return “Role [name=” + name + “, desc=” + desc + “]”;
-
}
-
}
-
package t2;
-
import org.junit.Test;
-
import utils.JaxbUtil;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:52:00
-
*/
-
public class JaxbTest2 {
-
@Test
-
public void showMarshaller() {
-
Student student = new Student();
-
student.setId(12);
面试资料整理汇总
这些面试题是我朋友进阿里前狂刷七遍以上的面试资料,由于面试文档很多,内容更多,没有办法一一为大家展示出来,所以只好为大家节选出来了一部分供大家参考。
面试的本质不是考试,而是告诉面试官你会做什么,所以,这些面试资料中提到的技术也是要学会的,不然稍微改动一下你就凉凉了
在这里祝大家能够拿到心仪的offer!
-
import org.junit.Test;
-
import utils.JaxbUtil;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:52:00
-
*/
-
public class JaxbTest2 {
-
@Test
-
public void showMarshaller() {
-
Student student = new Student();
-
student.setId(12);
面试资料整理汇总
[外链图片转存中…(img-DRkxvbtj-1715000939562)]
[外链图片转存中…(img-hyKv00Mr-1715000939563)]
这些面试题是我朋友进阿里前狂刷七遍以上的面试资料,由于面试文档很多,内容更多,没有办法一一为大家展示出来,所以只好为大家节选出来了一部分供大家参考。
面试的本质不是考试,而是告诉面试官你会做什么,所以,这些面试资料中提到的技术也是要学会的,不然稍微改动一下你就凉凉了
在这里祝大家能够拿到心仪的offer!