简体   繁体   中英

PUT method HTTP Status 400 in Spring MVC - AngularJS

I am trying to update the Company object, as you can see in company.js below. When I try to call the put method from company.js , it gives me the 400 Status error and the execution does not enter in the put method in CompanyController.java . Company object is also available in $scope. While executing the final URL in company.js is: http://localhost:8080/Jobkreisel/protected/company/50 but it does not even enter in the update method in CompanyController.java , just move to the error block in $http.put(url, $scope.company, config) method.

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;
    }
}

Please tell me why its not executing the success block.

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>

Network LOG:

Remote Address:127.0.0.1:8080 Request URL: http://localhost:8080/Jobkreisel/protected/company/50 Request Method:PUT Status Code:400 Bad Request Request Headersview source Accept: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 Host:localhost:8080 Origin: http://localhost:8080 Referer: 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 Request Payload view source

{companyID: "50", name: "Agrident GmbH", website: " http://www.agrident.com/ ", twitter: "",…} ausbildungvideourl: "" companyID: "50" employee: "" facebook: "" name: "Agrident GmbH" studiumvideourl: "" twitter: "" video: "" website: " http://www.agrident.com/ " Response Headers view source Content-Length:0 Pragma:no-cache Server:Jetty(6.1.21)

The web.xml is incomplete: indeed you need a servlet mapping of /Jobkreisel/* to your controllers.

Just replace the <url-pattern> tag of your spring dispatcher as follows:

<!-- 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>

You also could change the url called from your angular app if the mapping of the dispatcher is correct:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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