从Angular向Spring Controller发送JSON时出错 [英] Error while sending json from angular to spring controller

查看:55
本文介绍了从Angular向Spring Controller发送JSON时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在春季将来自angularjs服务的json数据发送到控制器.但是出现错误:

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"}

这是我在angularJs中使用的功能

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);
    }

我在角度服务中也有一个$ http.delete方法

Also I have a $http.delete method in angular service

$http.delete(REST_SERVICE_URI+"delete/"+id)

还有一个错误,说令牌.",预期的语法错误

更新

Product.java

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

更新:2

很抱歉,当我尝试发送空json时,发生NUllPointerException.Spring控制台中没有错误

Sorry the NUllPointerException was occuring when I tried to send empty json . There is no error in the spring console

MainController.java

MainController.java

@Controller
@RequestMapping("/")
public class MainController {
@Autowired
UserDao userDao;


@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public String defaultPage() {
    return "ProductManagement";

}

现在,angluar中的错误是:

Now the error in angluar is :

angular.js:10661 POST http://localhost:8080/shoping/product/add 404 (Not Found)

更新3:

当我发出get请求而不是post时,它没有给出错误,但是在数据库中添加了空值数据,并且它也不支持媒体类型json.现在,我认为问题很可能出在URI和http请求中.

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.

这是我在该项目的github仓库: https://github.com/Bk073/Shopping

Here is my github repo of the project : https://github.com/Bk073/Shopping

推荐答案

从大量信息(没有日志)来看,JDBC似乎有问题.如果一切都设置为默认值,那么我想 id 必须是 auto增量并向其发送 null ,而不会捕获任何异常成为原因...尝试从您的产品对象(json)中省略 id ,看看会发生什么...

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...

这篇关于从Angular向Spring Controller发送JSON时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆