[英]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.