Home  >  Forum  >  Spring
Post New Query

How to pass json subcontent as request parameter in Rest template exchange method?



Asked on October 11, 2017
I'm using spring boot microservice project. I am calling external server [UAA-Server] API using Rest Template from my microservice project.I have a rest api POST call that I need to send the body using raw, and I need to pass json subcontent as request parameter in Rest template exchange method. But I am getting following error: ERROR 5128 --- [nio-8084-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 null] with root cause org.springframework.web.client.HttpClientErrorException: 400 null However the following is worked in postman : curl 'http://localhost/Users' -i -X POST \ -H 'Accept: application/json' \ -H 'Authorization: Bearer f655182635eb4a279c88c605581d9344' \ -H 'Content-Type: application/json' \ -d '{ "externalId" : "test-user", "meta" : { "version" : 0, "created" : "2017-10-10T21:15:46.464Z" }, "userName" : "EbLzvz@test.org", "name" : { "formatted" : "given name family name", "familyName" : "family name", "givenName" : "given name" }, "emails" : [ { "value" : "EbLzvz@test.org", "primary" : true } ], "phoneNumbers" : [ { "value" : "5555555555" } ], "active" : true, "verified" : true, "origin" : "", "password" : "secret", "schemas" : [ "urn:scim:schemas:core:1.0" ] }' Below is what I tried in Java: @Override public String createUser(String accessToken, String subdomainName, String zoneid){ RestTemplate restTemplate = new RestTemplate(); String url = "http://localhost:8080/uaa/Users"; MultiValueMap headers = new LinkedMultiValueMap(); headers.add("Content-Type", MediaType.APPLICATION_JSON.toString()); headers.add("Authorization", "Bearer "+accessToken); headers.add("X-Identity-Zone-Id", zoneid); headers.add("X-Identity-Zone-Subdomain", subdomainName); Name objName = new Name(); objName.setFormatted("given name family name"); objName.setFamilyName("family name"); objName.setGivenName("given name"); System.out.println("Name data is:"+objName); Meta objMeta = new Meta(); objMeta.setVersion(0); objMeta.setCreated("2017-09-11T21:57:23.459Z"); Email objEmail = new Email(); objEmail.setValue("EbLzvz@test.org"); objEmail.setPrimary(true); System.out.println("Email data is:"+objEmail); PhoneNumber objPN = new PhoneNumber(); objPN.setValue("5555555555"); User objUser = new User(); objUser.setExternalId("testuser"); objUser.setMeta(objMeta); objUser.setUserName("EbLzvz@test.org"); objUser.setName(objName); objUser.setEmails(objEmail); objUser.setPhoneNumbers(objPN); objUser.setActive(true); objUser.setVerified(true); objUser.setOrigin(""); objUser.setPassword("secret"); objUser.setSchemas("urn:scim:schemas:core:1.0"); HttpEntity requestEntity = new HttpEntity(objUser, headers); ResponseEntity uri = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); return uri.getBody(); } How do I fix this? Thanks & Regards, Shilpa Kulkarni


Replied on October 11, 2017
Formatted Question:



I'm using spring boot microservice project. I am calling external server [UAA-Server] API using Rest Template from my microservice project.I have a rest api POST call that I need to send the body using raw, and I need to pass json subcontent as request parameter in Rest template exchange method. But I am getting following error:

ERROR 5128 --- [nio-8084-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 null] with root cause

org.springframework.web.client.HttpClientErrorException: 400 null

However the following is worked in postman :

curl 'http://localhost/Users' -i -X POST \

-H 'Accept: application/json' \

-H 'Authorization: Bearer f655182635eb4a279c88c605581d9344' \

-H 'Content-Type: application/json' \

-d '{

"externalId" : "test-user",

"meta" : {

"version" : 0,

"created" : "2017-10-10T21:15:46.464Z"

},

"userName" : "EbLzvz@test.org",

"name" : {

"formatted" : "given name family name",

"familyName" : "family name",

"givenName" : "given name"

},

"emails" : [ {

"value" : "EbLzvz@test.org",

"primary" : true

} ],

"phoneNumbers" : [ {

"value" : "5555555555"

} ],

"active" : true,

"verified" : true,

"origin" : "",

"password" : "secret",

"schemas" : [ "urn:scim:schemas:core:1.0" ]

}'

enter image description here

Below is what I tried in Java:

@Override public String createUser(String accessToken, String subdomainName, String zoneid){

    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8080/uaa/Users";
    MultiValueMap headers = new LinkedMultiValueMap();
    headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
    headers.add("Authorization", "Bearer "+accessToken);
    headers.add("X-Identity-Zone-Id", zoneid);
    headers.add("X-Identity-Zone-Subdomain", subdomainName);

    Name objName = new Name();
    objName.setFormatted("given name family name");
    objName.setFamilyName("family name");
    objName.setGivenName("given name");
    System.out.println("Name data is:"+objName);


    Meta objMeta = new Meta();
    objMeta.setVersion(0);
    objMeta.setCreated("2017-09-11T21:57:23.459Z");



    Email objEmail = new Email();
    objEmail.setValue("EbLzvz@test.org");
    objEmail.setPrimary(true);
    System.out.println("Email data is:"+objEmail);

    PhoneNumber objPN = new PhoneNumber();
    objPN.setValue("5555555555");


    User objUser = new User();
    objUser.setExternalId("testuser");
    objUser.setMeta(objMeta);
    objUser.setUserName("EbLzvz@test.org");
    objUser.setName(objName);
    objUser.setEmails(objEmail);
    objUser.setPhoneNumbers(objPN);
    objUser.setActive(true);
    objUser.setVerified(true);
    objUser.setOrigin("");
    objUser.setPassword("secret");
    objUser.setSchemas("urn:scim:schemas:core:1.0");


    HttpEntity requestEntity = new HttpEntity(objUser, headers);


    ResponseEntity uri = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);


    return uri.getBody();
}

How do I fix this?

Thanks & Regards,

Shilpa Kulkarni



Write Answer




©2019 concretepage.com | Privacy Policy | Contact Us