Spring MVC中的PUT方法HTTP Status 400 - AngularJS

时间:2015-03-03 11:04:34

标签: java javascript angularjs spring spring-mvc

我正在尝试更新公司对象,您可以在下面的company.js中看到。 当我尝试从 company.js 调用put方法时,它给出了 400状态错误,并且执行没有输入 CompanyController中的put方法。的java 即可。公司对象也可以在$ scope中使用。 在 company.js 中执行最终到达网址时: http://localhost:8080/Jobkreisel/protected/company/50 但它甚至没有输入 CompanyController.java 中的更新方法,只需移到 $ http.put(url,$ scope.company,config)中的错误块方法

company.js

  $scope.updateCompany = function (updateCompanyForm) {
        if (!updateCompanyForm.$valid) {
            $scope.displayValidationError = true;
            return;
        }
        $scope.lastAction = 'update';
        var url = '/Jobkreisel/protected/company/' + $scope.company.companyID;


        var config = {};

        alert("Company scope "+$scope.company.companyID);
        alert("Company config "+config);

        $http.put(url, $scope.company, config)
            .success(function (data) {

                alert('In update success');
            })
            .error(function(data, status, headers, config) {

                console.debug(data);
                alert('data:' + data);
                alert('status: ' + status); 

                alert('update error');
            });
    };

CompanyController.java

@Controller
@RequestMapping(value = "/protected/company")
public class CompanyController extends UserBaseController {

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "/{companyID}", method = RequestMethod.PUT, produces = "application/json")
    public ResponseEntity<?> update(@PathVariable("companyID") int companyId,
                                    @RequestBody Company company,                                    
                                    Locale locale) {
        if (companyId != company.getCompanyID()) {
            return new ResponseEntity<String>("Bad Request", HttpStatus.BAD_REQUEST);
        }

        companyService.save(company);

        return null;
    }
}

请告诉我为什么它没有执行成功阻止。

的web.xml

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

    <!-- Spring servlet that will handle the requests-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Spring basic configurations -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/spring.xml
        </param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Enconding helper filter -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <servlet-name>dispatcher</servlet-name>
    </filter-mapping>

    <!-- Encoding utility -->
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>
</web-app>

网络日志:

  

远程地址:127.0.0.1:8080请求   网址:http://localhost:8080/Jobkreisel/protected/company/50请求   方法:PUT状态代码:400 Bad Request Request Headersview source   接受:application / json,text / plain, / Accept-Encoding:gzip,   deflate,sdch Accept-Language:en-US,en; q = 0.8 Connection:keep-alive   Content-Length:175 Content-Type:application / json; charset = UTF-8   Cookie:JSESSIONID = 1w425u610rioe主持人:localhost:8080   起源:http://localhost:8080   引用者:http://localhost:8080/Jobkreisel/protected/company   User-Agent:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36   (KHTML,与Gecko一样)Chrome / 40.0.2214.115 Safari / 537.36请求   有效载荷视图源

     

{companyID:“50”,姓名:“Agrident GmbH”,网站:   “http://www.agrident.com/”,twitter:“”,...} ausbildungvideourl:“”   companyID:“50”员工:“”facebook:“”名称:“Agrident GmbH”   studiumvideourl:“”twitter:“”视频:“”网站:   “http://www.agrident.com/”响应标头查看源   Content-Length:0 Pragma:no-cache服务器:Jetty(6.1.21)

1 个答案:

答案 0 :(得分:0)

web.xml不完整:确实需要将/Jobkreisel/*的servlet映射到控制器。

只需更换弹簧调度程序的<url-pattern>标记,如下所示:

<!-- Spring servlet that will handle the requests-->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    ...
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/Jobkreisel/*</url-pattern>
</servlet-mapping>

如果调度程序的映射正确,您还可以更改从角应用程序调用的URL:

    var url = '/protected/company/' + $scope.company.companyID;