Issue
I have an application where server-side is in spring boot and client-side in angular. I want to send some data from client to spring boot controller but the data is not passing. Angular service code:
startProcess(env: string, processList: Array<any>): Observable<any> {
const params = new HttpParams().set('env', env).set('processList', JSON.stringify(processList));
return this.http.get<string>(this.starturl, {
params
})
.pipe(tap(), catchError(this.handleError));
}
Spring boot controller code:
public List<String> startProcess(
@RequestParam(required=false, defaultValue="Null") String environment,
@RequestParam(required=false, defaultValue="Null") List<String> processList
) {
// ...
}
The values are showing Null in the controller. The data is not getting passed properly. Please suggest how can I pass string and an Array of string from angular to spring boot controller and user my Array list(the format of list should be iterable like normal list).
Solution
Isn't it better to use POST to send your data to server?. May be change your controller something like below
@PostMapping(path = "/process")
public void getProcess(@RequestBody ProcessObj processObj){
//do anything with processObj
}
//Where ProcessObj is defined as below
public class ProcessObj{
private String env;
private List<String> processList;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public List<String> getProcessList() {
return processList;
}
public void setProcessList(List<String> processList) {
this.processList = processList;
}
}
And in Javascript
this.http.post(url, {"env":"ABC", "processList":["A", "B", "C"]}) You could refer more Angularjs samples here https://angular.io/guide/http#making-a-post-request
Answered By - Progen
Answer Checked By - Marilyn (JavaFixing Volunteer)