Jersey 无法使用 MOXy 解组对象(序言中不允许包含内容)
Jersey unable to unmarshal object with MOXy (Content is not allowed in prolog)
我使用 JAX-RS 2.0(泽西岛)实现了以下方法:
@PUT
@Path("container/{containername}/catalog")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response updateCatalog(@PathParam("containername") String containerName,
@FormDataParam("catalog") SIRFCatalog catalog) throws IOException, URISyntaxException {
log.info("Unmarshalling config...");
SIRFConfiguration config = new SIRFConfigurationUnmarshaller().
unmarshalConfig(new String(Files.readAllBytes(Paths.get(
SIRFConfiguration.SIRF_DEFAULT_DIRECTORY + "conf.json"))));
log.info("Creating strategy...");
StorageContainerStrategy strat = AbstractStrategyFactory.createStrategy(config);
log.info("Pushing catalog...");
strat.pushCatalog(catalog, containerName);
log.info("Sending response...");
return Response.ok(new URI("sirf/container/" + containerName + "/catalog")).build();
}
我正在使用 Eclipse MOXy,War 文件在与编译后的 class 相同的目录中有 jaxb.properties
文件。属性文件的内容是:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
当我发送一个包含 SIRFCatalog
的 XML 文件时,一切都按预期运行:
curl -i -X PUT -H "Content-Type:multipart/form-data" -F catalog=@a.xml http://$OPENSIRF_IP:$OPENSIRF_PORT/sirf/container/philContainer/catalog
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 48
Date: Sat, 10 Sep 2016 18:30:30 GMT
{"value":"sirf/container/philContainer/catalog"}
但是,当我在 JSON 中发送完全相同的内容时(我知道它们是相同的,因为我将相同的对象编组到 XML 和 JSON 并保存到 a.xml
和 a.json
) 我得到一个 HTTP 400:
curl -i -X PUT -H "Content-Type:multipart/form-data" -F catalog=@a.json http://$OPENSIRF_IP:$OPENSIRF_PORT/sirf/container/philContainer/catalog
HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1033
Date: Sat, 10 Sep 2016 18:30:38 GMT
Connection: close
<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.36 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 400 - Bad Request</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Bad Request</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><hr class="line"><h3>Apache Tomcat/8.0.36</h3></body></html>
我怀疑我的服务器实际上使用的是 Vanilla JAXB 而不是 MOXy,但不知道如何调试它! tomcat8 日志显示 Content not allowed in prolog
:
10-Sep-2016 18:30:36.902 INFO [http-nio-8080-exec-21] org.glassfish.jersey.filter.LoggingFilter.log 2 * Server has received a request on thread http-nio-8080-exec-21
2 > PUT http://200.144.189.109:8088/sirf/container/philContainer/catalog
2 > accept: */*
2 > content-length: 1755
2 > content-type: multipart/form-data; boundary=----------------------------bb3a9e312931
2 > expect: 100-continue
2 > host: 200.144.189.109:8088
2 > user-agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
[Fatal Error] :1:1: Content is not allowed in prolog.
10-Sep-2016 18:30:38.386 INFO [http-nio-8080-exec-21] org.glassfish.jersey.filter.LoggingFilter.log 2 * Server responded with a response on thread http-nio-8080-exec-21
2 < 400
有什么想法吗?
However, when I send the very same contents in JSON (and I know they
are the same because I marshalled the same object to XML and JSON and
saved to a.xml and a.json) I get an HTTP 400:
我怀疑生成的 json
有根节点 node
(就像在 XML
中一样),因此您必须指示 MOxy
不要使用根节点。请按照 Blaise 的解释检查此 link and link。
更新:
是的,您需要在 cURL 命令中指定数据类型,如下所示:
-F "catalog=@a.json;type=application/json"
我使用 JAX-RS 2.0(泽西岛)实现了以下方法:
@PUT
@Path("container/{containername}/catalog")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response updateCatalog(@PathParam("containername") String containerName,
@FormDataParam("catalog") SIRFCatalog catalog) throws IOException, URISyntaxException {
log.info("Unmarshalling config...");
SIRFConfiguration config = new SIRFConfigurationUnmarshaller().
unmarshalConfig(new String(Files.readAllBytes(Paths.get(
SIRFConfiguration.SIRF_DEFAULT_DIRECTORY + "conf.json"))));
log.info("Creating strategy...");
StorageContainerStrategy strat = AbstractStrategyFactory.createStrategy(config);
log.info("Pushing catalog...");
strat.pushCatalog(catalog, containerName);
log.info("Sending response...");
return Response.ok(new URI("sirf/container/" + containerName + "/catalog")).build();
}
我正在使用 Eclipse MOXy,War 文件在与编译后的 class 相同的目录中有 jaxb.properties
文件。属性文件的内容是:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
当我发送一个包含 SIRFCatalog
的 XML 文件时,一切都按预期运行:
curl -i -X PUT -H "Content-Type:multipart/form-data" -F catalog=@a.xml http://$OPENSIRF_IP:$OPENSIRF_PORT/sirf/container/philContainer/catalog
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 48
Date: Sat, 10 Sep 2016 18:30:30 GMT
{"value":"sirf/container/philContainer/catalog"}
但是,当我在 JSON 中发送完全相同的内容时(我知道它们是相同的,因为我将相同的对象编组到 XML 和 JSON 并保存到 a.xml
和 a.json
) 我得到一个 HTTP 400:
curl -i -X PUT -H "Content-Type:multipart/form-data" -F catalog=@a.json http://$OPENSIRF_IP:$OPENSIRF_PORT/sirf/container/philContainer/catalog
HTTP/1.1 100 Continue
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1033
Date: Sat, 10 Sep 2016 18:30:38 GMT
Connection: close
<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.36 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 400 - Bad Request</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>Bad Request</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><hr class="line"><h3>Apache Tomcat/8.0.36</h3></body></html>
我怀疑我的服务器实际上使用的是 Vanilla JAXB 而不是 MOXy,但不知道如何调试它! tomcat8 日志显示 Content not allowed in prolog
:
10-Sep-2016 18:30:36.902 INFO [http-nio-8080-exec-21] org.glassfish.jersey.filter.LoggingFilter.log 2 * Server has received a request on thread http-nio-8080-exec-21
2 > PUT http://200.144.189.109:8088/sirf/container/philContainer/catalog
2 > accept: */*
2 > content-length: 1755
2 > content-type: multipart/form-data; boundary=----------------------------bb3a9e312931
2 > expect: 100-continue
2 > host: 200.144.189.109:8088
2 > user-agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
[Fatal Error] :1:1: Content is not allowed in prolog.
10-Sep-2016 18:30:38.386 INFO [http-nio-8080-exec-21] org.glassfish.jersey.filter.LoggingFilter.log 2 * Server responded with a response on thread http-nio-8080-exec-21
2 < 400
有什么想法吗?
However, when I send the very same contents in JSON (and I know they are the same because I marshalled the same object to XML and JSON and saved to a.xml and a.json) I get an HTTP 400:
我怀疑生成的 json
有根节点 node
(就像在 XML
中一样),因此您必须指示 MOxy
不要使用根节点。请按照 Blaise 的解释检查此 link and link。
更新: 是的,您需要在 cURL 命令中指定数据类型,如下所示:
-F "catalog=@a.json;type=application/json"