I'm trying to save an json array to service in back-end via $http.post but I always get 400 Bad Request
. I have googled all around and there's not a clear answer to be found.
My questions are follows:
1. Is it possible to post json array via $http.post?
2. If yes, how it is done?
3. If no, what other alternatives there would be to save json array with such aim?
To make this even more clear, I'm going to add some of my codes below which are necessary.
At Front-end
SavingsService
createBatchSavings: function(savingsBatch){
return $http.post('/finance/savings/create/batchsavings', savingsBatch).success(function(data){
return data;
});
}
Above function gets it's savingsBatch parameter properly. savingsBatch is json array.
At Back-end
SavingServiceImpl
@Component
@Path("/savings")
@Produces("application/json")
public class SavingsServiceImpl implements SavingsService {
@Autowired
FinanceEntityDao financeDao;
@POST
@Path("/create")
public FinanceEntity createSaving(FinanceEntity financeEntity) {
financeEntity.setType(FinanceType.SAVING);
financeEntity.setDateAdded(new Date());
return financeDao.save(financeEntity);
}
@POST
@Path("/create/batchsavings")
public List<FinanceEntity> createBatchSavings(List<FinanceEntity> savingsBatch) {
if(savingsBatch.size() > 0){
return financeDao.massSave(savingsBatch);
}
else {
throw new NullPointerException("Saving batch was null!");
}
}
}
I have also examined the header values. Request headers is having proper values but Response headers content-length is always zero.
This problem is finally solved. There were two problems in this implementation. First one in front-end and second in back-end.
Front-end
First problem was that I tried to post json array straightly to Java service class, which won't work, because it must be converted into proper string json array. Code to send the json array from controller to service is something like this:
$scope.createBatchSavings = function(batchSavings){
return SavingsService.createBatchSavings(batchSavings).success(function(data){
swal("Saving batch saved!", "Saving batch was saved successfully!", "success");
$scope.savingsBatch = [];
});
}
Here batchSavings is json array which content-type in header was application/json. This must be stringified and thus above method must be written like below in order to pass proper json array to back-end service:
$scope.createBatchSavings = function(batchSavings){
return SavingsService.createBatchSavings(JSON.stringify(batchSavings)).success(function(data){
swal("Saving batch saved!", "Saving batch was saved successfully!", "success");
$scope.savingsBatch = [];
});
}
Back-end
Secondly this stringified array must be deserialized in the back-end. Previously the connection between front-end service and back-end service didn't even work because of passing inappropriate json array. So the connection was fixed by converting the json array into proper one.
In order for back-end service to work I had to change the parameter type from List<FinanceEntity>
into String
. Also I had to choose deserialization "machine". In my case I chose Gson. Thus below method:
@POST
@Path("/create/batchsavings")
public List<FinanceEntity> createBatchSavings(List<FinanceEntity> savingsBatch) {
if(savingsBatch.size() > 0){
return financeDao.massSave(savingsBatch);
}
else {
throw new NullPointerException("Saving batch was null!");
}
}
becomes:
@POST
@Path("/create/batchsavings")
public List<FinanceEntity> createBatchSavings(String savingsBatch) {
Gson gson = new Gson();
Type collectionType = new TypeToken<List<FinanceEntity>>(){}.getType();
List<FinanceEntity> savingsList = gson.fromJson(savingsBatch, collectionType);
if(savingsList.size() > 0){
return financeDao.massSave(savingsList);
}
else {
throw new NullPointerException("Saving batch was null!");
}
}
Now this whole thing works.
Just as a notification, if you have any dates you must format them into proper format because otherwise you will encounter Unparsable date
problem in Java.