I am trying to make a http call from angularjs in spring controller and post json data but I am getting this error in angularjs :
angular.js:10661 POST http://localhost:8080/shopng/product/add 404 (Not
Found)
(anonymous) @ angular.js:10661
sendReq @ angular.js:10480
serverRequest @ angular.js:10187
processQueue @ angular.js:14634
(anonymous) @ angular.js:14650
$eval @ angular.js:15916
$digest @ angular.js:15727
$apply @ angular.js:16024
(anonymous) @ angular.js:23416
eventHandler @ angular.js:3293
product_service.js:35 Error while adding product
product_controller.js:30 {productName: "ads", productPrice: 34, description: "ads", imageUrl: "asd"}
But when I use GET request then it's not giving me any error but it says media type not supported and when I remove the media type and perform then 'NULL' value is added in database.
Here is my RestController:
@RestController
@RequestMapping("/product")
public class ProductRestController {
@Autowired
ProductDao productDao;
//Add Product
@RequestMapping(value = "/add", method= RequestMethod.POST, consumes="application/json")
public ResponseEntity<Void> createProduct(@ModelAttribute("product") Product product) {
System.out.println("Creating Product " + product.getProductName());
if (productDao.isProductExit(product)) {
System.out.println("A Product with name " + product.getProductName() + " already exist");
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
productDao.add(product);
return new ResponseEntity<Void>(HttpStatus.CREATED);
}
Here is my angular's service making request:
function addProduct(product){
var deferred = $q.defer();
$http.post('http://localhost:8080/shopng/product/add', product)
.then(
function(response){
deferred.resolve(response.data);
},
function(errResponse){
console.log('Error while adding product');
deferred.reject(errResponse);
}
);
return deferred.promise;
}
here is my github repo of the project : https://github.com/Bk073/Shopping Any help would be appreciated.
I have gone through your securityconfig class.
Please disable the csrf as below.
You are getting 404 - because the csrf check is throwing an exception. To handle that exception, you have not added an exception handler. So the 404 means - exception handler is not found, not the post request itself.
If you need to secure with csrf, keep in mind to send csrf token with the request.
Please see the below attachment with successful response.
updated code https://github.com/supun/Shopping
Try something like this. Add RequestBody instead of ModelAttribute.
The value property in your controller is incorrect. It should start with / . Please change to /add in your controller