使用REST在角度json中出现500内部服务器错误

时间:2015-01-12 11:08:10

标签: java angularjs rest

嗨,伙计们,我正在解决这个问题,希望我能在这里得到一些帮助。

我的问题: - 我正在使用角度js作为前端,并使用rest api将其连接到后端。在我的一些部分内部角js或我们可以说一些HTML页面,我能够成功GET,但只有一个HTML页面或部分GET说内部服务器错误500 .i尝试调试我的浏览器以及显示的是angular.js。 - GET localhost:8080 / mytracker / getDeviceList.do 500(内部服务器错误) -

困惑: - 我的困惑是角度js代码或我的后端代码是问题,这是我在spring写的rest api代码。问题是因为我得到了text / html格式的数据 .for 其他html页面我能够进入application / json 。可能是我后端的问题。

发布我的后端代码: -

DeviceController.java - >

@Controller
public class DeviceController {

@Autowired DeviceDAO deviceDAO;

@RequestMapping(value="/getDeviceList",method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<DeviceForm> getdevice()
{
    List<DeviceForm> deviceForms=new ArrayList<DeviceForm>();
    List<Device> devices= deviceDAO.getAllDevices();
    for(Device device:devices)
    {
        DeviceForm deviceForm= new DeviceForm();
        deviceForm.setDeviceNumber(device.getDeviceNo());
        deviceForm.setDeviceId(device.getDeviceId());
        deviceForm.setPurchaseDate(device.getPurchaseData().toString());
        deviceForm.setExpiryDate(device.getExpiryDate().toString());
        deviceForm.setClientId(device.getDeviceClienid());

        deviceForms.add(deviceForm);
    }
    return deviceForms; 
}

我的自动装配如下: -

public interface DeviceDAO {

public List<Device> getAllDevices();

public void saveDevice(Device device);
}

我的 DeviceForm类: - &gt;

private double deviceId;
private String purchaseDate;
private String warrantyPeriod;
private String warEndDate;
private String clientId;
private String warrantedDays;
private String warStartDate;
private String warranty;
private String deviceNumber;
private String expiryDate;
private long vehicleId;
private String deviceStatus;


public double getDeviceId() {
    return deviceId;
}
public void setDeviceId(double deviceId) {
    this.deviceId = deviceId;
}
public long getVehicleId() {
    return vehicleId;
}
public void setVehicleId(long vehicleId) {
    this.vehicleId = vehicleId;
}
public String getExpiryDate() {
    return expiryDate;
}
public void setExpiryDate(String expiryDate) {
    this.expiryDate = expiryDate;
}
public String getDeviceNumber() {
    return deviceNumber;
}
public void setDeviceNumber(String deviceNumber) {
    this.deviceNumber = deviceNumber;
}
public String getWarranty() {
    return warranty;
}
public void setWarranty(String warranty) {
    this.warranty = warranty;
}

public String getPurchaseDate() {
    return purchaseDate;
}
public void setPurchaseDate(String purchaseDate) {
    this.purchaseDate = purchaseDate;
}
public String getWarrantyPeriod() {
    return warrantyPeriod;
}
public void setWarrantyPeriod(String warrantyPeriod) {
    this.warrantyPeriod = warrantyPeriod;
}
public String getWarEndDate() {
    return warEndDate;
}
public void setWarEndDate(String warEndDate) {
    this.warEndDate = warEndDate;
}

public String getWarrantedDays() {
    return warrantedDays;
}
public void setWarrantedDays(String warrantedDays) {
    this.warrantedDays = warrantedDays;
}
public String getWarStartDate() {
    return warStartDate;
}
public void setWarStartDate(String warStartDate) {
    this.warStartDate = warStartDate;
}
public String getClientId() {
    return clientId;
}
public void setClientId(String clientId) {
    this.clientId = clientId;
}

}

我在角色前端中使用这些休息类,使用在角度控制器中编写的代码,如下所示: - &gt;

myApp.controller("DeviceManagementController",['$scope','$http',function($scope,$http){ 
$scope.gridOptions = {
        pagingPageSizes: [25, 50, 75],
        pagingPageSize: 25,
        columnDefs: [
                    { name: 'deviceId' },
                    { name: 'deviceNumber' },
                    { name: 'purchaseDate' },
                    { name: 'expiryDate' }
        ],



 $http.get('http://localhost:8080/mytracker/getDeviceList.do')
  .success(function (data) {
    $scope.gridOptions.data = data;
  });

}]);

我的完整stackTrace是: -

Jan 12, 2015 5:11:15 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [mytracker] in context with path [/mytracker] threw       exception [Request processing failed; nested exception is java.lang.NullPointerException] with root    cause
java.lang.NullPointerException
at com.dataisys.mytracker.web.controller.DeviceController.getdevice(DeviceController.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at     org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodA    dapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
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.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
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.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
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.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)

希望得到建议!!!!

2 个答案:

答案 0 :(得分:0)

信息很清楚。

你有一个NPE:在你的getdevice方法中。

java.lang.NullPointerException
at com.dataisys.mytracker.web.controller.DeviceController.getdevice(DeviceController.java:44)

看起来deviceDAO为null。

答案 1 :(得分:0)

这种情况正在发生,因为您从 getExpiryDate()获得的结果为空。因此,请确保数据库表中您从中获取数据的列不为空。然后我想一切都会好起来的。