Error while sending json from angular to spring co

2019-08-20 10:21发布

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

1条回答
戒情不戒烟
2楼-- · 2019-08-20 10:30

From this amount of information (no logs) it seems there must be something wrong with JDBC. If everything has been set as default, then I suppose that the id must be auto increment and sending it a null without catching any exceptions just might be the cause... try to omit id from your product object (json) and see what happens...

查看更多
登录 后发表回答