Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将卷曲字符串转换为Resteasy_Java_Http_Post_Curl_Resteasy - Fatal编程技术网

Java 将卷曲字符串转换为Resteasy

Java 将卷曲字符串转换为Resteasy,java,http,post,curl,resteasy,Java,Http,Post,Curl,Resteasy,我无法将示例cURL字符串(有效)转换为有意义的resteasy表达式。旋度是: curl -X POST -u admin:admin --data-binary "@tempfile.xml" http://localhost:8810/rest/configurations 我有: public void sendPost(ConfigurationDTO config) throws JAXBException, FileNotFoundException { // clien

我无法将示例cURL字符串(有效)转换为有意义的resteasy表达式。旋度是:

curl -X POST -u admin:admin --data-binary "@tempfile.xml" http://localhost:8810/rest/configurations
我有:

public void sendPost(ConfigurationDTO config) throws JAXBException, FileNotFoundException {
    // client target is:  http://localhost:8810/rest
    ResteasyWebTarget target = getTarget();
    target.path("configurations");
    JAXBContext context = JAXBContext.newInstance(ConfigurationDTO.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    // this here produces exactly what I need
    marshaller.marshal(config, new File("test.xml"));

    MultipartFormDataOutput dataOutput = new MultipartFormDataOutput();
    dataOutput.addFormData("file", new FileInputStream(new File("test.xml")), MediaType.APPLICATION_OCTET_STREAM_TYPE);
    GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(dataOutput) {};


    Response response = target.request().post( Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
    response.close();
}

protected ResteasyWebTarget getTarget() {
    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target(UriBuilder.fromUri(restUrl).build());
    client.register(new AddAuthHeadersRequestFilter(user, pass));
    return target;
}
public void sendPost(ConfigurationDTO config)抛出jaxbeexception、FileNotFoundException{
//客户目标是:http://localhost:8810/rest
ResteasyWebTarget=getTarget();
目标路径(“配置”);
JAXBContext context=JAXBContext.newInstance(ConfigurationDTO.class);
Marshaller=context.createMarshaller();
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
//这正是我需要的
marshaller.marshall(配置,新文件(“test.xml”);
MultipartFormDataOutput dataOutput=新的MultipartFormDataOutput();
dataOutput.addFormData(“文件”、新文件InputStream(新文件(“test.xml”)、MediaType.APPLICATION\uOctet\uStream\uType);
GenericeEntity实体=新的GenericeEntity(dataOutput){};
Response Response=target.request().post(Entity.Entity(Entity,MediaType.MULTIPART_FORM_DATA_TYPE));
response.close();
}
受保护的ResteasyWebTarget getTarget(){
ResteasyClient客户端=新的ResteasyClientBuilder().build();
ResteasyWebTarget=client.target(UriBuilder.fromUri(restUrl.build());
注册(新AddAuthHeadersRequestFilter(用户,通过));
回报目标;
}

抛出HTTP.500,我无法访问服务器以查看发生了什么

我会尝试在cURL中调试cor(请参阅)。在您的情况下,它是一个命令:

curl --verbose -u admin:admin \
  -H "Origin: http://localhost:1234" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: X-Requested-With" \
  -X OPTIONS \
  http://localhost:8810/rest/configurations
请将
localhost:1234
替换为运行RESTEasy客户端的上下文路径


如果请求未成功,则表示CORS配置存在问题。如果响应包含
访问控制允许来源
标头,则请求成功。

cURL在使用
--data binary
参数时发送
内容类型:application/x-www-form-urlencoded
。您使用的是
MediaType.MULTIPART\u FORM\u DATA\u TYPE(MULTIPART/FORM DATA)
,因此我希望您的服务器不接受后者。然后RESTeasy将抛出
javax.ws.rs.NotSupportedException:无法使用内容类型

我不明白为什么要将实体编组为文件并将此文件传递给RESTeasy客户端。例如,使用
StringWriter
您的代码可能如下所示:

StringWriter sw = new StringWriter();
marshaller.marshal(config, sw);
Response response = target.request().post(Entity.entity(sw.toString(), MediaType.APPLICATION_FORM_URLENCODED));

服务器部分也是由您编写的吗?如果您只发送xml文件,则
应用程序/x-www-form-urlencoded
似乎不是最佳匹配的内容类型。

首先,我在您的代码中缺少用户密码信息。它是在
getTarget()
中完成的,还可以获取请求,所以没问题。我是否应该理解,您可以通过RESTEasy客户端调用GET请求,并且它可以工作?是的。GET没有问题,按预期工作。客户端采用相同的方法组成(添加到原始帖子中)。仅POST-s return HTTP.500您的RESTEasy客户端是否在与REST服务不同的端口上运行?如果是这样,您是否只为GET请求配置了CORS头?cURL调用正在工作,问题在于RESTeasy代码。此外,这里可能没有CORS问题,因为curl和RESTeasy客户端都没有同源策略。感谢您的输入。写入文件主要是为了调试目的。服务器不是我写的,它就像一个黑盒子,我只是在我们使用它的时候测试它。星期一我会试试你的建议。