1

I need to URL-encode a nested object for a REST-API call via POST. My problem is, its not a plain structure.

public class Transaction{
    private Integer merchantID = xx; 
    private String storeID = "xxxxx"; 
    private String orderID;
    private String userID;
    private Boolean autoCapture = true;
    private String context = "ONLINE";
    private String userType = "PRIVATE";
    private Integer userRiskClass = 0;
    private Person userData = new Person();
    private Address billingAddress = new Address();
    private Amount amount;
    private List<BasketItem> basketItems;
    private String locale = "DE";
    private String mac;
}

If I try

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

HttpEntity<Transaction> entity = new HttpEntity<>(transaction, headers);

I recieve org.springframework.web.client.RestClientException: No HttpMessageConverter for de.crefo.paymentportalbackend.restcall.crefopay.modals.Transaction and content type "application/x-www-form-urlencoded"

If I try to convert it to a MultiValueMap it's not producing valid JSON anymore

I tried both, just adding the objects to the map:

public MultiValueMap<String, Object> toMultiValueMap(){

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

    map.add("merchantID", String.valueOf(merchantID));
    map.add("storeID", storeID);
    map.add("orderID", orderID);
    map.add("userID", userID);
    map.add("autoCapture", String.valueOf(autoCapture));
    map.add("context", context);
    map.add("userType", userType);
    map.add("userRiskClass", String.valueOf(userRiskClass));
    map.add("userData", userData);
    map.add("billingAddress", billingAddress);
    map.add("amount", amount);
    map.add("locale", locale);
    map.add("mac", mac);

    return map;
}

result after decode:

merchantID=xx& 
storeID=xxxx& 
orderID=123.456.789-12& 
userID=123.456.789-12& 
autoCapture=true& 
context=ONLINE& 
userType=PRIVATE& 
userRiskClass=0& 
userData=Person(name=Energieversorgung+Mittelrhein,+surname=AG,+email=info@evm.de)& 
billingAddress=Address(street=Ludwig-Erhard-Strasse+8,+zip=56073,+city=Koblenz,+country=DE)& 
amount=Amount(amount=9999)& 
basketItem=[BasketItem(basketItemText=Offene+Forderung,+basketItemCount=1,+basketItemAmount=Amount(amount=9999))]& 
locale=DE& 
mac=a3a97061553e0889af0064222014826a7df28037

and converting the objects self:

public MultiValueMap<String, Object> toMultiValueMap(){

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

    map.add("merchantID", String.valueOf(merchantID));
    map.add("storeID", storeID);
    map.add("orderID", orderID);
    map.add("userID", userID);
    map.add("autoCapture", String.valueOf(autoCapture));
    map.add("context", context);
    map.add("userType", userType);
    map.add("userRiskClass", String.valueOf(userRiskClass));
    map.add("userData", userData.toMultiValueMap());
    map.add("billingAddress", billingAddress.toMultiValueMap());
    map.add("amount", amount.toMultiValueMap());
    List<MultiValueMap<String, String>> basketItems2 = new ArrayList<>();
    basketItems.forEach(basketItem -> basketItems2.add(basketItem.toMultiValueMap()));
    map.add("basketItem", basketItems2);
    map.add("locale", locale);
    map.add("mac", mac);

    return map;
}

result after decode:

merchantID=xx& 
storeID=xxxx& 
orderID=123.456.789-12& 
userID=123.456.789-12& 
autoCapture=true& 
context=ONLINE& 
userType=PRIVATE& 
userRiskClass=0& 
userData={name=[Energieversorgung+Mittelrhein],+surname=[AG],+email=[info@evm.de]}& 
billingAddress={street=[Ludwig-Erhard-Strasse+8],+zip=[56073],+city=[Koblenz],+country=[DE]}& 
amount={amount=[9999]}& 
basketItem=[{basketItemText=[Offene+Forderung],+basketItemCount=[1],+basketItemAmount=[Amount(amount=9999)]}]& 
locale=DE& 
mac=a3a97061553e0889af0064222014826a7df28037]

it should more look like:

amount={"amount":1000}& 
context=ONLINE& 
orderID=5d430b3c69e5e& 
userID=8533fc039f64a8bbab656ff29331399e& 
userType=PRIVATE& 
basketItems=[{"basketItemText":"Order","basketItemCount":1,"basketItemAmount":{"amount":1000}}]& 
locale=DE& 
integrationType=API& 
userRiskClass=1& 
merchantID=xx& 
storeID=xxxx

Is there a way to URL encode nested objects for RestTemplate or do I have to use the other way to call the REST-API?

Let me know if need any further information!

Eardy
  • 125
  • 13
  • 1
    https://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jackson !? You should pass the data as json. – luk2302 Aug 02 '19 at 07:39
  • 1
    I don't get your answer. the expected result it not just JSON and it needs to be URL-encoded. – Eardy Aug 02 '19 at 08:01
  • Possible duplicate: https://stackoverflow.com/questions/28753773/could-not-write-request-no-suitable-httpmessageconverter-found-for-request-type – Sana Aug 02 '19 at 09:32
  • Did you find a good way to do this? – Mustafa Jan 27 '22 at 06:25

0 Answers0