I am trying to send json data from angularjs service to controller in spring. But getting error:
angular.js:10661 POST http://localhost:8080/shoping/product/add 500 (Internal Server Error)
product_service.js:35 Error while adding product
product_controller.js:30 {productId: null, productName: "sdfv", productPrice: 43, description: "sfdv", imageUrl: "csd"}
This is my function in service in angularJs
function addProduct(product){
var deferred = $q.defer();
$http.post(REST_SERVICE_URI+"add", product)
.then(
function(response){
deferred.resolve(response.data);
},
function(errResponse){
console.log('Error while adding product');
deferred.reject(errResponse);
}
);
return deferred.promise;
}
This is my method in spring controller
@RestController
@RequestMapping("/product/")
public class ProductRestController {
@Autowired
ProductDao productDao;
RequestMapping(value = "add", method= RequestMethod.POST, consumes="application/json")
public ResponseEntity<Void> createProduct(@RequestBody 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);
}
Also I have a $http.delete method in angular service
$http.delete(REST_SERVICE_URI+"delete/"+id)
And there is also an error saying Syntax error on token ".", , expected
UPDATE
Product.java
@Entity
@Table(name ="product")
public class Product implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="product_id")
private int productId;
@Column(name = "product_name")
private String productName;
@Column(name = "product_price")
private Float productPrice;
@Column(name = "description")
private String description;
@Column(name = "image_url")
private String imageUrl;
//getter and setter
UPDATE: 2
Sorry the NUllPointerException was occuring when I tried to send empty json . There is no error in the spring console
MainController.java
@Controller
@RequestMapping("/")
public class MainController {
@Autowired
UserDao userDao;
@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public String defaultPage() {
return "ProductManagement";
}
Now the error in angluar is :
angular.js:10661 POST http://localhost:8080/shoping/product/add 404 (Not Found)
UPDATE 3:
When I make get request instead of post then it is not giving error but adding empty value data in database and it also doesn't support media type json. Now I think that most probably the problem is in the URI and http request.
Here is my github repo of the project : https://github.com/Bk073/Shopping