繁体   English   中英

Spring MVC中的PUT方法HTTP状态400-AngularJS

[英]PUT method HTTP Status 400 in Spring MVC - AngularJS

我正在尝试更新Company对象,如下面的company.js所示。 当我尝试从company.js调用put方法时,它给了我400 Status错误 ,并且执行未在CompanyController.java的put方法中输入。 公司对象在$ scope中也可用。 company.js中执行最终URL时: http:// localhost:8080 / Jobkreisel / protected / company / 50但它甚至没有输入CompanyController.java中的update方法,只需移至$ 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请求URL: http:// localhost:8080 / Jobkreisel / protected / company / 50请求方法:PUT状态码:400错误的请求请求标题视图源接受:application / json,text / plain, / Accept-Encoding:gzip,deflate,sdch Accept-Language:zh-cn,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,like 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语法:no-cache服务器:码头(6.1.21)

web.xml是不完整的:确实,您需要/Jobkreisel/*到服务器的servlet映射。

只需按如下所示替换spring调度程序的<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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM