gpt4 book ai didi

java - JAX-WS:javax.xml.bind.UnmarshalException

转载 作者:行者123 更新时间:2023-11-30 07:06:04 26 4
gpt4 key购买 nike

//HelloWorld.java
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
@Path("generic")
public class HelloWorld {
@Context
private UriInfo context;
public HelloWorld() {
}
@GET
@Produces("text/html")
public String getHtml() {
return "Hello World";
}
}

//ApplicationConfig.java
import java.util.Set;
import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(HelloWorld.class);
}
}

//JavaApplication3
package javaapplication3;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import sun.misc.IOUtils;
public class JavaApplication3 {
public static void main(String[] args) throws MalformedURLException, IOException, JAXBException {
//Get the URI by selecting the RESTful web services folder under the web app project. Then right click on the underlying node
//and select: TestResourceURI
String uri ="http://localhost:8080/HelloRestService/webresources/generic";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
//connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Accept", "text/html");
JAXBContext jc = JAXBContext.newInstance(String.class);
InputStream xml = connection.getInputStream();
String str = (String) jc.createUnmarshaller().unmarshal(xml); //line that throws exception
connection.disconnect();
}
}

我已经标记了导致异常的行:JavaApplication3.java。我可以在这里浏览网络服务:http://localhost:8080/HelloRestService/webresources/generic

异常(exception)情况是:

Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:41)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1465)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:999)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:504)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
... 4 more
Java Result: 1

有什么问题吗?我希望 str 变量填充为 HelloWorld!。

最佳答案

您收到此错误是因为您尝试使用 JAXB 绑定(bind)甚至不是 XML 内容的内容,而该内容根本无法工作(提醒 JAXB 代表 Java Architecture for XML Binding )。您真正得到的是 plain/text 甚至不是您在代码中设置的 text/html,而 JAXB 需要 XML >。

您需要做的是将输入流转换为 String ,如下所示:

try (
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream(), charset)
)
) {
String content = reader.lines().collect(Collectors.joining("\n"));
...
}

关于java - JAX-WS:javax.xml.bind.UnmarshalException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40108576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com