angularjs - spring开机 - spring data/rest

angularjs - springboot - spring data/rest

我正在使用 angular-js/spring boot/sprint data/rest

开始个人项目

我在数据库中保存实体对象时遇到问题

@Entity
@Table(name="CUSTOMER")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="CUSTOMER_ID")
private int customerId;

@Column(name="NAME")
private String name;

@Column(name="PHONE_NUMBER")
private String phoneNumber;

@Column(name="POSTAL_ADDRESS")
private String postalAddress;

@Column(name="CITY")
private String city;

@Column(name="COUNTRY")
private String country;

@Column(name="EMAIL")
private String email;

@OneToMany(mappedBy="customer")
@OrderBy("name ASC")
private List<Contact> contacts;

@ManyToOne
@JoinColumn(name = "STATUS_ID")
private RefStatus status;

RefStatus.java

@Entity
@Table(name="REF_STATUS")
public class RefStatus {

@Id
@Column(name="STATUS_ID")
private int statusId;

@Column(name="CODE")
private String code;

@Column(name="DESCRIPTION")
private String description;

@Column(name="ACTIVE")
private Boolean active;

CrudRepository

@RepositoryRestResource(collectionResourceRel = "refstatus", path = "refstatus")
public interface RefStatusRepository extends CrudRepository<RefStatus, Integer> {
     @Query("SELECT c FROM RefStatus c")
     List<RefStatus> findAll();
}

app.js

$scope.addCustomer = function addCustomer() {
    if($scope.name==""){
        alert("Error");
    }
    else{
         $http.post(urlBase + '/customers', {

         name: $scope.name,
         status : $scope.statusId
     }).
      success(function(data, status, headers) {
  alert("Customer added");
         var newTaskUri = headers()["location"];
            });
}
};

html 页面

<div ng-controller="taskManagerController">
<div id="add-task-panel" class="fadein fadeout addpanel panel">
    <div class="panel-body">
        <div class="task" >
            <table class="add-task">
                <tr>
                    <td>Customer Name:</td>
                    <td><input type="text" ng-model="name"/></td>
                </tr>

                <tr>
                    <td>Status:</td>
                    <td>


                <select ng-model="statusId" ng-options="status.code for status in items.refstatus">
                            <option value="">-- Select --</option>                      
                            </select>

                    </td>
                </tr>
                <tr>
                    <td><br/><button ng-click="addCustomer()" class="btn-panel-big">Ajouter client 3</button></td>
                </tr>
            </table>                                
        </div>
    </div>
    </div>

当我点击按钮 "addcustomer" 时,出现了这个异常:

2015-01-02 23:17:53.033 ERROR 11108 --- [p-nio-8-exec-10]    s.d.r.w.AbstractRepositoryRestController : Could not read JSON: (was java.lang.NullPointerException)   (through reference chain: fr.stockmanagem
ent.Customer["status"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: fr.stockmanagement.Customer["status"])

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: (was java.lang.NullPointerException) (through reference chain: fr.stockmanagement.Customer["status"]);  nested e
xception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: fr.stockmanagement.Customer["status"])
at    org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJacks on2HttpMessageConverter.java:228)
at   org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJack   son2HttpMessageConverter.java:212)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:159)
at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:100)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1720)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was   java.lang.NullPointerException) (through reference chain: fr.stockmanagement.Customer["status"])
at    com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1420)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at   org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJacks  on2HttpMessageConverter.java:225)
... 39 common frames omitted
Caused by: java.lang.NullPointerException: null
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at java.net.URI.create(Unknown Source)
at   org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:359)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
... 43 common frames omitted

您知道如何保存包含对象 (RefStatus) 的对象 Customer 吗?

谢谢,

我找到了解决办法。

我在 Java 中创建了一个控制器,输入来自 app.js 的所有属性。

     @RequestMapping(value="/create/{name}/{status}",method = RequestMethod.POST,headers="Accept=application/json")
 public Customer create(
         @PathVariable String name,
         @PathVariable String status){
     Customer customer = new Customer();
     customer.setName(name);

    RefStatus refStatus = refStatusRepository.findByCode(status);
    customer.setStatus(refStatus);

    return this.customerRepository.save(customer);
 }

我不知道这是否是最好的解决方案,但它工作正常:)