Home  >  Forum  >  Spring
Post New Query

Resttemplate 400 null exception



Asked on April 19, 2018
Im working on resttemplate post method.I need to post json string in this format,

 {
"capabilities": [
"upload-license",
"publish-app-to-dashboard",
"access-all-models-in-tenant",
"tenant-admin",
"access-reports",
"access-editor",
"upload-license" 
]
}

But when i post from spring Resttemplate it showing me 400 null. 

I have created model for this.

modelname=Group

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
  "name",
  "status",
  "tenantId"})
   
public class Group
{
  
  private List<Capabilities> capabilities;
  
  String tenantId;
  
  String status;
  
  String name;
  
  String cap_id;
  
  String groupId;
  
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getCap_id() {
return cap_id;
}
public void setCap_id(String cap_id) {
this.cap_id = cap_id;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public void setCapabilties(List<Capabilities> capabilities) {
    if (capabilities!=null && capabilities.size()>0) {
        ArrayList<Capabilities> list = new ArrayList<Capabilities>();
        list.addAll(capabilities);
        for (int i=(list.size()-1); i>=0; i--) {
         Capabilities pn = list.get(i);
            if (pn==null || (!hasText(pn.getName()))) {
                list.remove(i);
            }
        }
        capabilities = list;
    }
    this.capabilities = capabilities;
}

public List<Capabilities> getCapabilities() {
return capabilities;
}


public static final class Capabilities {
    private String name;
    public Capabilities(String capabilities) {
        this.name = capabilities;
    }

    public Capabilities() {}

    public String getName() {
        return name;
    }

    public void setName(String capabilities) {
        this.name = capabilities;
    }

    @Override 
public String toString() {
return "{capabilities=" + name 
+ "}";
}

}
}



Below code is my method.
 
  
@Override
public String addCapability(String groupId, String cap_id) 
{
try {
System.out.println("groupId"+groupId);
System.out.println("cap_id"+cap_id);
Query query = 
em.createQuery("SELECT aps.external_aps_capabilities_group_id FROM Usergroup aps where aps.external_uaa_groupid='"+groupId+"'");
System.out.println("query"+query.toString());
String activiti_groupid = (String) query.getSingleResult();
System.out.println("activiti_grpid"+activiti_groupid);
String url = activiti_URL+"/api/enterprise/admin/groups/"+activiti_groupid+"/capabilities";
String jsonString = "";

ObjectMapper mapper = new ObjectMapper();
        
System.out.println("cap_id"+cap_id);
System.out.println("url"+url);
    Group.Capabilities capabilities=new Group.Capabilities();
capabilities.setName(cap_id);
List<Group.Capabilities> name =new ArrayList<Group.Capabilities>();
name.add(capabilities); 

System.out.println("groupId"+groupId);
RestTemplate restTemplate = new RestTemplate();
 
    Group objGroup = new Group();
    objGroup.setCapabilties(name);
jsonString = mapper.writeValueAsString(objGroup);
System.out.println("objGroup"+objGroup);
System.out.println("jsonString"+jsonString);
   
    HttpEntity<String> requestEntity = new HttpEntity<String>(jsonString, headers2);

String result = HttpRestClient.restTemplatePostMethod(requestEntity, url);
System.out.println("result="+result);
return result;   
} catch (Exception ex) {
System.out.println("exception=="+ex.getMessage());
        }
return "error";
}  

    
    
When i work on this im getting Exception like this,


result=400 null
addCapability==400 null

    
    



Replied on April 19, 2018
Go through the URL 


May be this will help you.


Write Answer








©2019 concretepage.com | Privacy Policy | Contact Us