当我运行marshal操作时,我收到以下错误:
javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]
...
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
... 6 more
Run Code Online (Sandbox Code Playgroud)
这是我编组的功能......
public StringBuffer Marshaller(Object marshall){ // make marshalling->Java to XML
StringWriter writer = new StringWriter();
try {
JAXBContext jaxbContext=JAXBContext.newInstance(marshall.getClass());
Marshaller jaxbMarshaller=jaxbContext.createMarshaller();
// ç?kt?
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(marshall, writer);
System.out.println(writer.getBuffer().toString()); …Run Code Online (Sandbox Code Playgroud) 我是 Spring Boot 的新手并构建了我的第一个简单项目,但在运行应用程序时遇到了问题。
你能告诉我为什么它在下面给出错误吗?
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.javabrains.springbootquickstart</groupId>
<artifactId>course-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java Brains Course API</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
Run Code Online (Sandbox Code Playgroud)
CourseApiApp.java
package io.javabrains.springbootstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这个截图属于我的例子。
控制台输出如下:应用程序无法启动。“在类路径中添加一个实现,例如 Hibernate Validator”
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ …Run Code Online (Sandbox Code Playgroud)