Java Spring MVC+;休息+;找不到AngularJs 404

Java Spring MVC+;休息+;找不到AngularJs 404,java,angularjs,rest,spring-mvc,spring-restcontroller,Java,Angularjs,Rest,Spring Mvc,Spring Restcontroller,使用Json调用REST控制器时遇到问题。获取404错误。但是没有发现问题。控制器已定义。没有拼写错误 另外,要使用REST,我们需要在web.xml或servlet-config.xml中进行配置。 我在依赖项中有jackson数据绑定和jax-rs 春天的菜鸟。我需要帮助 我正在尝试调用“/addintoexp”控制器文件 文件:expense.js 'use strict'; var addexp=angular.module('emsExpenseApp',[ ]); addexp

使用Json调用REST控制器时遇到问题。获取404错误。但是没有发现问题。控制器已定义。没有拼写错误

另外,要使用REST,我们需要在web.xml或servlet-config.xml中进行配置。 我在依赖项中有jackson数据绑定和jax-rs

春天的菜鸟。我需要帮助

我正在尝试调用“/addintoexp”控制器文件

文件:expense.js

'use strict';

var addexp=angular.module('emsExpenseApp',[ ]);


addexp.controller('addExpCtrl',[ '$scope', '$http', '$location' ,function($scope,$http,$location){  
    console.log("Reached the Angular controller");

    var vm=this;
    vm.expense={};

    vm.expense.expense_name="GROFERS MONTHLY";

    // add-expense button implementation

    vm.submit=function(){
        console.log("Inside Submit Function");
        console.log(vm.expense);
         $http({
             method: 'POST',
             url: addintoexp,
             data: vm.expense,
             dataType: 'application/json',
             headers: {'Content-Type': 'application/json'}
            }).success(function(response) {
                alert(' Success '); 
                console.dir(response); 

                //alert('Success'); 
                // redirect path
                //$location.path('dashboard/users');

            }).error(function(response){ 


                alert('Error');


            });


 }

}]);
文件:ExpenseController.java

package com.ems.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ems.model.Expense;
import com.ems.model.User;
import com.sun.media.jfxmedia.logging.Logger;
/**
 * 
 * @author Ankit
 *
 * In  this controller , methods like add_expense, del_expense, view_expense, edit_expense
 * all accordingly to the user_id
 *
 *
 *
 */
@RestController
public class ExpenseController {

    @RequestMapping(value="/add-exp", method=RequestMethod.GET)
    public ModelAndView addExpense(){
        ModelAndView m=new ModelAndView("add-expense");

        return m;
    }


    /*
    @RequestMapping(value="/addintoexp", method=RequestMethod.POST)
    public String addExpense(@RequestParam("exp-name") String expname,
            @RequestParam("exp-desc") String expdesc,
            @RequestParam("exp-amount") String expamount,
            @RequestParam("exp-date") String expdate,
            @RequestParam("exp-notes") String expnotes,
            @RequestParam("exp-category") String expcategory
            ){



        System.out.println(
                " Expense Name : "+expname +
                " Expense Desc : "+expdesc +
                " Expense Amount : " +expamount+
                " Expense Date : "+expdate+
                " Expense Notes : "+expnotes+
                " Expense Category : "+expcategory 



                );

        return "redirect:/add-exp.html";

    }

    */

    //404 Error getting for this controller
    @RequestMapping(value = "/addintoexp",method = RequestMethod.POST,headers="Accept=application/json")
    public ResponseEntity<Void> addexpenser(@RequestBody Expense e){

        System.out.println("Inside the Adding Exp Controller. Hurrah. REST is working");
        System.out.println(e.getExpense_name());
        System.out.println(e.getExpense_category());
        System.out.println(e.getExpense_desc());

        HttpHeaders header = new HttpHeaders();

        return new ResponseEntity<Void>(header,HttpStatus.CREATED);

    }

    //Rest API Add Expense  
}
package com.ems.controller;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.MediaType;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.ResponseBody;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.servlet.ModelAndView;
导入com.ems.model.Expense;
导入com.ems.model.User;
导入com.sun.media.jfxmedia.logging.Logger;
/**
* 
*@作者安基特
*
*在此控制器中,可以使用添加费用、删除费用、查看费用、编辑费用等方法
*所有这些都对应于用户id
*
*
*
*/
@RestController
公共类费用控制员{
@RequestMapping(value=“/add exp”,method=RequestMethod.GET)
公共模型和视图添加费用(){
ModelAndView m=新的ModelAndView(“添加费用”);
返回m;
}
/*
@RequestMapping(value=“/addintoexp”,method=RequestMethod.POST)
公共字符串addExpense(@RequestParam(“exp name”)字符串expname,
@RequestParam(“exp desc”)字符串expdesc,
@RequestParam(“exp amount”)字符串expamount,
@RequestParam(“exp-date”)字符串expdate,
@RequestParam(“exp notes”)字符串expnotes,
@RequestParam(“exp类别”)字符串expcategory
){
System.out.println(
“费用名称:”+expname+
“费用说明:”+expdesc+
“费用金额:”+EXPANTUM+
“费用日期:”+expdate+
“费用注释:”+expnotes+
“费用类别:”+expcategory
);
返回“重定向:/add-exp.html”;
}
*/
//404获取此控制器时出错
@RequestMapping(value=“/addintoexp”,method=RequestMethod.POST,headers=“Accept=application/json”)
公共响应费用增加者(@RequestBody Expense e){
System.out.println(“在添加Exp控制器内。好哇。REST正在工作”);
System.out.println(e.getExpense_name());
System.out.println(例如getExpense_category());
System.out.println(e.getExpense_desc());
HttpHeaders header=新的HttpHeaders();
返回新的ResponseEntity(标题,HttpStatus.CREATED);
}
//RESTAPI添加费用
}
文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <servlet>
    <servlet-name>expTrackerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

        <servlet-mapping>
        <servlet-name>expTrackerServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
        </servlet-mapping>  
  <display-name>Archetype Created Web Application</display-name>

</web-app>

expTrackerServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/config/servlet-config.xml
1.
expTrackerServlet
*.html
Web应用程序创建的原型
文件:Servlet-Config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=" http://www.springframework.org/schema/beans 
                      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                      http://www.springframework.org/schema/context 
                      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                      http://www.springframework.org/schema/mvc 
                      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <mvc:resources location="/static" mapping="/static/**"/>

 <mvc:annotation-driven/>
 <context:component-scan base-package="com.ems"/> 

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
 </bean>

 <bean id="expenseDaoImpl" class="com.ems.dao.ExpenseDaoImpl">
        <property name="dataSource" ref="dataSource" />
 </bean>

 <bean id="loginDaoImpl" class="com.ems.dao.LoginDaoImpl">
        <property name="dataSource" ref="dataSource" />
 </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/exms" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>


    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
 </beans>


您正在使用变量
addintoexp
作为URL发送请求,但该变量未在任何地方定义。打开浏览器开发工具及其网络面板,您将看到发送的内容和时间。另外,为什么要将一个servlet映射为承载返回JSON的RESTful API到
*.html
?最后,升级您的angularjs版本:您使用的是旧版本,因为最近的版本在HTTP承诺中没有任何成功/错误方法。使用AngularJS启动一个新项目已经不是一个好主意,但是使用AngularJS的旧版本,情况更糟。将AngularJS中的url更改为url:'addintoexp.html',但出现406错误我使用jackson进行数据绑定您希望从REST端点获得什么?现在您正在发回一个
JSP
视图,这不是REST背后的意图。使用REST,您通常会发送表示域资源的JSON对象。