javascript
java 获取400的错误信息_获取400错误的请求Spring RestTemplate POST
我想使用POST方法使用Spring Rest web服務。我無法在客戶端(網(wǎng)站)訪問POJO文件,因此必須在客戶端使用JSON并在Web服務中使用POJO。下面是我的代碼:獲取400錯誤的請求Spring RestTemplate POST
REST控制器
@RequestMapping(value="/updateAdmin", method=RequestMethod.POST, consumes="application/json", headers="Accept=application/json")
public ResponseEntity updateAdmin(@RequestBody Ss_admin admin) {
try {
ss_admin_dao.updateAdmin(admin);
} catch(Exception ex) {
ex.printStackTrace();
}
return new ResponseEntity(HttpStatus.CREATED);
}
POJO
package model;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Ss_admin implements Serializable {
@JsonProperty("a_id")
private long a_id;
@JsonProperty("a_username")
private String a_username;
@JsonProperty("a_password")
private String a_password;
public Ss_admin() {}
public long getA_id() {
return a_id;
}
public void setA_id(long a_id) {
this.a_id = a_id;
}
public String getA_username() {
return a_username;
}
public void setA_username(String a_username) {
this.a_username = a_username;
}
public String getA_password() {
return a_password;
}
public void setA_password(String a_password) {
this.a_password = a_password;
}
}
REST客戶
try {
RestTemplate restTemplate = new RestTemplate();
List> list = new ArrayList>();
list.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(list);
//restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build()));
String url = "http://localhost:8181/xyz/updateAdmin";
JSONArray json = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("a_username", "testabcd");
obj.put("a_id", 1);
obj.put("a_password", "N/A");
json.put(obj);
HttpHeaders headers = new HttpHeaders();
//headers.setContentType(MediaType.APPLICATION_JSON);
//headers.setAccept(MediaType.APPLICATION_JSON);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
//headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity entity = new HttpEntity(obj.toString(), headers);
restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
//restTemplate.postForEntity(url, entity, String.class);
} catch(Exception ex) {
ex.printStackTrace();
}
正如你在評論中看到的那樣,我嘗試了很多選擇但沒有成功。
堆棧跟蹤
Aug 04, 2017 6:58:12 PM org.springframework.web.client.RestTemplate handleResponseError
WARNING: POST request for "http://localhost:8181/xyz/updateAdmin" resulted in 400 (Bad Request); invoking error handler
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:576)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:532)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:431)
at webcontroller.TestController.update(TestController.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
編輯 我曾測試REST Web服務http://localhost:8181/xyz/updateAdmin 使用郵差,并且它成功執(zhí)行,但不與RestTemplate工作。
問題解決 請參閱下面的答案。
+0
我也有使用RestTemplate的方法postForObject錯誤請求400。原因是在我的pojo,我沒有使用@JsonProperty來注釋每個字段?;旧?#xff0c;在修復之前,Pojo的內容沒有被發(fā)送到端點。如果您在郵遞員中刪除了json請求的主體,則驗證了這一點。發(fā)送一個空的身體也給你一個400.這是線索,然后在pojo狹窄。 –
總結
以上是生活随笔為你收集整理的java 获取400的错误信息_获取400错误的请求Spring RestTemplate POST的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java arrays.equals_J
- 下一篇: Java实现话术词槽匹配_知识图谱与KB