Spring REST PUT form data

Postman image:

REST client:

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;

	@SuppressWarnings("unchecked")
	private void testPost() {

		String url = "http://localhost:8080/uds-transaction-1.3.0/pipeline/list/";
		RestTemplate restTemplate = new RestTemplate();

		List<Long> list = new ArrayList<Long>();
		list.add(new Long(30284039614L));
		list.add(new Long(30262601669L));
		list.add(new Long(30262662522L));

		UdsTransactionRequest req = new UdsTransactionRequest();
		req.setTrxIdsLong(list);

		HttpEntity<UdsTransactionRequest> request = new HttpEntity<>(req);

		ServiceResponse<UfsPipeline> response = restTemplate.postForObject(url, request, ServiceResponse.class);

		List<UfsPipeline> results = response.getData();
		if (results != null && !results.isEmpty()) {
			for (UfsPipeline a : results) {
				System.out.println("---------------- txNo: " + a.getTxNo());
			}
		}
	}

Endpoint:

	@RequestMapping(value = "/pipeline/list/", produces = "application/json", method = RequestMethod.POST)
	public ServiceResponse<UfsPipeline> getOfficeDocumentEndPoint(@RequestBody UdsTransactionRequest req) {
		ServiceResponse<UfsPipeline> response = new ServiceResponse<>();
		try {
			List<UfsPipeline> list = pipelineRepo.getPipelinesByKeys(req.getTrxIdsLong());
			response.setData(list);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			response.addError(e);
		}
		return response;
	}

Support class:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.ns.model.DataCacheMeta;
import com.ns.model.notification.UfsPipeline;

@JsonInclude(Include.NON_NULL)
public class ServiceResponse<T extends ResponseData> implements Serializable {

	public static final String OK = "ok";
	public static final String ERROR = "error";

	private static final long serialVersionUID = 4579157541957745675L;
	private List<ResponseError> errors;
	private List<T> data;
	private String status = OK;

	public void addError(Exception e) {
		if (null == errors) {
			errors = new ArrayList<>();
		}
		ResponseError error = new ResponseError(null, ResponseError.ERROR, e.getMessage());
		errors.add(error);
		setStatus(ServiceResponse.ERROR);
	}

	public void addData(T dataElement) {
		if (null == data) {
			data = new ArrayList<>();
		}
		data.add(dataElement);
	}

	@JsonSubTypes({@JsonSubTypes.Type(value = ResponseError.class, name = "error")})
	public List<ResponseError> getErrors() {
		return errors;
	}

	public void setErrors(List<ResponseError> errors) {
		this.errors = errors;
	}

	@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
	@JsonSubTypes({ @JsonSubTypes.Type(value = DataCacheMeta.class, name = "DataCacheMeta"),
			@JsonSubTypes.Type(value = UfsPipeline.class, name = "UfsPipeline")
	})
	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
}

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.