无法使用角度和弹簧触发有效的请求
在Spring中使用角度的REST Web服务时,我无法对其执行任何操作,因为以下代码是我的代码@cartRestcontroller
@RestController
@RequestMapping("/rest/cart")
public class CartRestController {
@RequestMapping(value = "/add/{productId}", method = RequestMethod.PUT)
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public void addItem(String productId, HttpServletRequest request) {
String sessionId = request.getSession(true).getId();
Cart cart = cartService.read(sessionId);
if (cart == null)
cart = cartService.create(cart);
Product product = productService.getProductById(productId);
System.out.println(product.getName());
if (product == null)
throw new IllegalArgumentException("Product does not exist");
cart.addCartItem(new CartItem(product));
cartService.update(sessionId, cart);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Please verify your payload")
public void handleServerErrors(Exception ex) {
ex.printStackTrace(System.out);
}
我的这个工作的控制器
var cartApp = angular.module('cartApp', []);
cartApp.controller('cartCtrl', function ($scope, $http) {
console.log("Inside Controller");
$scope.refreshCart = function(cartId) {
$http.get('/webstore_ecommerce/rest/cart/'+$scope.cartId)
.success(function(data) {
$scope.cart = data;
});
};
$scope.clearCart = function() {
$http.delete('/webstore_ecommerce/rest/cart/'+$scope.cartId)
.success($scope.refreshCart($scope.cartId));
};
$scope.initCartId = function(cartId){
$scope.cartId=cartId;
$scope.refreshCart($scope.cartId);
};
$scope.addToCart = function(productId) {
$http.put('/webstore_ecommerce/rest/cart/add/'+productId)
.success(function(data) {
$scope.refreshCart($http.get('webstore_ecommerce/rest/cart/get/cartId'));
conslole.log("Sucess, added to cart");
alert("Product Successfully added to the Cart!");
});
};
$scope.removeFromCart = function(productId) {
$http.put('/webstore_ecommerce/rest/cart/remove/'+productId)
.success(function(data) {
$scope.refreshCart($http.get('/webstore_ecommerce/rest/cart/get/cartId'));
});
};
});
和我的addTocart按钮
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script src="/webstore_ecommerce/resource/js/controllers.js"></script>
<title>Products</title>
</head>
<body>
<section>
<div class="jumbotron">
<div class="container">
<h1>Products</h1>
</div>
</div>
</section>
<section class="container" ng-app="cartApp">
<div class="row">
<div class="col-md-5">
<img
src="<c:url value="/resource/images/${product.productId}.png"></c:url>"
alt="image" style="width: 100%" />
</div>
<div class="col-md-5">
<h3>${product.name}</h3>
<p>${product.description}</p>
<p>
<strong>Item Code : </strong><span class="label label-warning">${product.productId}</span>
</p>
<p>
<strong>manufacturer</strong> : ${product.manufacturer}
</p>
<p>
<strong>category</strong> : ${product.category}
</p>
<p>
<strong>Condition</strong> : ${product.condition}
</p>
<p>
<strong>Availble units in stock </strong> : ${product.unitsInStock}
</p>
<h4>${product.unitPrice}USD</h4>
<p ng-controller="cartCtrl">
<a href="#" class="btn btn-warning btn-large"
ng-click="addToCart('${product.productId}')"> <span
class="glyphicon-shopping-cart glyphicon"></span> Order Now
</a> <a href="<spring:url value="/cart" />" class="btn btn-default">
<span class="glyphicon-hand-right glyphicon"></span> View Cart
</a> <a href="<spring:url value="/products" />" class="btn btn-default">
<span class="glyphicon-hand-left glyphicon"></span> back
</a>
</p>
</div>
</div>
</section>
</body>
这是我现在正在获取的错误
Ë
rror:conslole未定义$ scope.addToCart@http:// localhost:8080 / webstore_ecommerce / resource / js / controllers.js:25:3 Lc / u / <@https://ajax.googleapis.com/ajax/ libs / angularjs / 1.0.1 / angular.min.js:70:32 cc [c] https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:138:373 Rc / this。$ gethttps://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:85:412 Rc / this。$ gethttps://ajax.googleapis.com/ajax /libs/angularjs/1.0.1/angular.min.js:86:1 cc [c] https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:138: 353 oc / c / <@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:23:13 m @ https://ajax.googleapis.com/ajax/ libs / angularjs / 1.0.1 / angular.min.js:6:191 oc / c @ https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js:22:490 c / <()angular.min.js(第60行)Cc / this。$ get
... n:c(“warn”),info:c(“info”),error:c(“error”)}}]} function Jc(b,a){function c(a){re .. 。
编辑如果我使用邮递员发送请求,一切工作正常编辑这是我从stacktrace获得的错误
java.lang.NullPointerException
at com.local.domain.repo.impl.CartRepositiryImpl.create(CartRepositiryImpl.java:22)
at com.local.domain.service.impl.CartServiceImpl.create(CartServiceImpl.java:18)
at com.local.domain.comtrollers.CartRestController.addItem(CartRestController.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:745)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
我的cartRepositiry
@Repository公共类CartRepositiryImpl实现CartRepositiry {
Map<String, Cart> listOfCarts;
public CartRepositiryImpl() {
listOfCarts = new HashMap<>();
}
@Override
public Cart create(Cart cart) {
if (listOfCarts.containsKey(cart.getCartId()))
throw new IllegalArgumentException(String.format("Cart with id : (%s) already exists", cart.getCartId()));
listOfCarts.put(cart.getCartId(), cart);
return cart;
}
这就是萤火虫的回报:
“NetworkError:500 - http:// localhost:8080 / webstore_ecommerce / rest / cart / add / P1235”P1235由于我对此不熟悉,请帮助我解决此问题,
谢谢
你写的conslole而不是控制台...你最好去睡觉或喝咖啡休息时间:)
===> conslole.log("Sucess, added to cart");
链接地址: http://www.djcxy.com/p/77703.html