JAXB参考实现不支持JSON,您需要添加一个包,例如Jackson或Moxy
莫西
//import org.eclipse.persistence.jaxb.JAXBContextProperties; Map<String, Object> properties = new HashMap<String, Object>(2); properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); JAXBContext jc = JAXBContext.newInstance(new Class[] {User.class}, properties); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(user, System.out);杰克逊
//import org.prehaus.jackson.map.AnnotationIntrospector;//import org.prehaus.jackson.map.ObjectMapper;//import org.prehaus.jackson.xc.JaxbAnnotationIntrospector;ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());mapper.setAnnotationIntrospector(introspector);String result = mapper.writevalueAsString(user);
在这里查看示例



