Jersey无法使用MOXy解组对象(prolog中不允许使用内容)

时间:2016-09-10 18:40:29

标签: java jaxb jersey moxy

我使用JAX-RS 2.0(Jersey)实现了以下方法:

@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文件的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.xmla.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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

  

然而,当我在JSON中发送相同的内容时(我知道它们   是相同的,因为我将同一个对象编组为XML和JSON   保存到a.xml和a.json)我得到一个HTTP 400:

我怀疑生成的json有根node(如XML中所示),因此您必须指示MOxy不使用根节点。请按照Blaise的说明检查此linklink

更新: 是的,您需要在cURL命令中指定数据类型,如下所示:

-F "catalog=@a.json;type=application/json"