Groovy 将字符串上载到restful客户端

Groovy 将字符串上载到restful客户端,groovy,Groovy,我正在尝试使用java将字符串上载到restful webclient。客户机设置为使用文件路径接收多种格式的数据 除了创建文件外,还有其他方法上载此字符串吗?我可以创建一个byte[]对象,还是需要创建一个临时文件来上传它 例如: String s = "<tag>test</tag>" String uri = "http://someserver.com/entity" HttpHeaders httpHeaders = new HttpHeaders() http

我正在尝试使用java将字符串上载到restful webclient。客户机设置为使用文件路径接收多种格式的数据

除了创建文件外,还有其他方法上载此字符串吗?我可以创建一个byte[]对象,还是需要创建一个临时文件来上传它

例如:

String s = "<tag>test</tag>"
String uri = "http://someserver.com/entity"
HttpHeaders httpHeaders = new HttpHeaders()
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA)

LinkedMultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>()
    formParams.add('1', s)

HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(formParams, httpHeaders)
ResponseEntity<Void> result = keystoreRestTemplate.exchange(uri, HttpMethod.POST, requestEntity, Void.class)

我不确定我是否理解正确,但您可以发布一个查询字符串,就像使用get一样。多样性不重要

def address = 'http://posttestserver.com/post.php'.toURL()
def connection = address.openConnection()
connection.setRequestMethod("POST")
connection.doOutput = true

def writer = new OutputStreamWriter(connection.outputStream)
writer.write('firstname=Test&lastname=Test2')
writer.flush()
writer.close()

connection.connect()
print connection.content.text

您需要调用restful webclient的url,它是GET还是POST?您是用纯Java编写此代码的?春天使用Apache的HttpClient?抱歉,不清楚。我试图把它弄清楚。这是一个POST请求。而且,它是Groovy,而不仅仅是Java。那么,我如何处理文件呢?我想我的问题不清楚。。。后端似乎需要一个参数文件。我已经通过groovy生成了文件的内容,并希望上传这些内容。我是否被迫为此制作临时文件?
def address = 'http://posttestserver.com/post.php'.toURL()
def connection = address.openConnection()
connection.setRequestMethod("POST")
connection.doOutput = true

def writer = new OutputStreamWriter(connection.outputStream)
writer.write('firstname=Test&lastname=Test2')
writer.flush()
writer.close()

connection.connect()
print connection.content.text