Spring REST PUT byte array

POM.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.0.RELEASE</version>
</dependency>

API


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

private void test() {

	byte[] bytes = readBytesFromFile(path);
	// Prepare acceptable media type
	List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
	acceptableMediaTypes.add(MediaType.ALL);

	String uri = "http://localhost:8080/upload/{company}/{terminal}/{user}/{docType}/{docNum}/{trx}/?addendum=false&index=0&channel=ui";
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

	// Prepare header
	HttpHeaders headers = new HttpHeaders();
	headers.setAccept(acceptableMediaTypes);
	HttpEntity<byte[]> entity = new HttpEntity<byte[]>(bytes, headers);

	Map<String, String> param = new HashMap<String, String>();
	param.put("company", "750");
	param.put("terminal", "7501");
	param.put("user", "cmanshande");
	param.put("docType", "HAWS");
	param.put("docNum", "20222608918");
	param.put("trx", "750110714862");

	// Send the request as PUT
	restTemplate.exchange(uri, HttpMethod.PUT, entity, byte[].class, param);
}

Client

@RequestMapping(value = "/upload/{company}/{terminal}/{user}/{docType}/{txId}/**", method = RequestMethod.PUT)
@ResponseBody
public ResponseEntity<String> processUpload(
		@RequestBody byte[] content,
		@PathVariable String company, 
		@PathVariable String terminal,
		@PathVariable String user, 
		@PathVariable String docType,
		@PathVariable String txId, 
		HttpServletRequest httpRequest) {
.
.
.
.
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.