我正在使用角度js前端和后端弹簧启动应用程序。我希望通过向表userHotels中插入数据来影响酒店用户,这是表gui-user和SystemDefHotel之间的关系ManyToMany。我的问题是当我从前到后发送数据时我得到:error 500()NullPointerException这意味着后端从前端收到空值
这是我的类UsersHotels:
public class UsersHotels implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private User user;
private SystemDefHotel hotel;
// additional fields
private boolean linked;
public UsersHotels() {
super();
}
public UsersHotels(long id, User user, SystemDefHotel hotel, boolean linked) {
this.id = id;
this.user = user;
this.hotel = hotel;
this.linked = linked;
}
public UsersHotels(User user, SystemDefHotel hotel) {
super();
this.user = user;
this.hotel = hotel;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_Hotel_ID")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne(cascade = CascadeType.REFRESH)
@JoinColumn(name = "USER_ID")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne(cascade = CascadeType.REFRESH)
@JoinColumn(name = "HOTEL_CODE")
public SystemDefHotel getHotel() {
return hotel;
}
public void setHotel(SystemDefHotel hotel) {
this.hotel = hotel;
}
public boolean isLinked() {
return linked;
}
public void setLinked(boolean linked) {
this.linked = linked;
}
我的函数service.java影响酒店用户:
@Override
public void affectHotelToUser(UsersHotels userhotel) {
SystemDefHotel hotel= hotelDao.findHotelByName1(userhotel.getHotel().getHotelName());
userhotel.setHotel(hotel);
User user= dao.findById(userhotel.getUser().getUserId());
userhotel.setUser(user);
dao.saveUserHotel(userhotel);
}
我的controller.java:
@RequestMapping(value = "/affectHotelToUser", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<Void> affectHotelToUser(@RequestBody UsersHotels userHotel ) {
SystemDefHotel us=userHotel.getHotel();
User h=userHotel.getUser();
UsersHotels uh= new UsersHotels(h, us);
usersService.affectHotelToUser(uh);
return new ResponseEntity<Void>(HttpStatus.OK);
}
在前角js:这是我的service.js:
service.affectHotelUser = function(userHotel){
console.log("User: ", userHotel.User );
console.log("SystemDefHotel: ", userHotel.hotel );
return $http.post(_contextPath + '/affectHotelToUser', userHotel).then(
function(response) {
return response.data;
}, function(errResponse) {
console.log("user Hotel error 1: ", userHotel.hotel );
console.log("user Hotel error 2: ", userHotel.User );
console.error('Error while sending data');
return $q.reject(errResponse);
});
};
controller.js:
$scope.nameHotelSelected={};
$scope.affectHotelUser = function(){
console.log("newUser : ",$scope.newUser);
var userHotel={
User:$scope.newUser,
linked:true,
hotel: $scope.nameHotelSelected
};
console.log("userId : ",$scope.newUser.userId);
console.log("hotelCode : ",$scope.nameHotelSelected.code);
UsersService.affectHotelUser(userHotel).then( function(d) {
$scope.users = d
console.log("userscwvcb", $scope.users);
},
function(errResponse){
console.error('Error while adding userHotel');
});
};
和我的观点:
<table class="detailsHotelLink" cellspacing="1" >
<tr >
<td align="right" style="height: 35%;width: 10.5%" >Add new link :</td>
<td style="width: 500%;" >
<select type="text" class="form-control" ng-model="nameHotelSelected" placeholder="contact" ng-options="nameHotel.name for nameHotel in hotels" ng-change="changedValue(this)" >
<option value="">Select Account</option>
</select>
</td>
</tr>
<tr >
<td align="right" style="height: 32%"> Hotel : </td>
<td><a href="#" data-toggle="modal" data-target="#addHotelUser" >{{nameHotelSelected.name}}</a>
<i class="fa fa-pencil"></i>
<i class="fa fa-trash-o" ng-click="deleteHotel()"></i>
<div class="modal fade" id="addHotelUser" aria-hidden="false" data-backdrop="true" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h5 class="">Edit Link</h5>
</div>
<div class="modal-body">
<div class="form-group" >
<div class="group text-center">
<input type="text" ng-model="nameHotelSelected.name" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-green" data-dismiss="modal">Cancel</button>
<button type="button" id="btn1" class="btn btn-default" ng-click="affectHotelUser()" >Save</button>
</div>
</div>
</td>
这是我的堆栈跟踪:
janv. 03, 2018 10:40:44 AM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/neorchaWEBPlatform] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.lbc.takeoff.neorcha.business.service.UsersServiceImpl.affectHotelToUser(UsersServiceImpl.java:385)
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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy97.affectHotelToUser(Unknown Source)
at com.lbc.takeoff.neorcha.web.controller.UsersController.affectHotelToUser(UsersController.java:314)
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:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.lbc.takeoff.neorcha.web.configuration.CORSFilter.doFilter(CORSFilter.java:61)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:157)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:168)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
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:748)
我很抱歉这个问题,但实际上我仍然没有找到解决方案。任何帮助都要提前感谢。