java 获取 Spring RestTemplate POST 的 400 个错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45508547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:43:30  来源:igfitidea点击:

Getting 400 Bad Request for Spring RestTemplate POST

javajsonrestspring-mvcresttemplate

提问by Jignesh M. Khatri

I want to consume Spring Rest web-service, using POST method. I don't have access to POJO file at client end (website), so have to use JSON at client end and POJO at web-service. Below is my code:

我想使用 POST 方法使用 Spring Rest Web 服务。我无权访问客户端(网站)的 POJO 文件,因此必须在客户端使用 JSON,在 Web 服务使用 POJO。下面是我的代码:

REST Controller

休息控制器

@RequestMapping(value="/updateAdmin", method=RequestMethod.POST, consumes="application/json", headers="Accept=application/json")
public ResponseEntity<String> updateAdmin(@RequestBody Ss_admin admin) {
    try {
        ss_admin_dao.updateAdmin(admin);
    } catch(Exception ex) {
        ex.printStackTrace();
    }
    return new ResponseEntity<String>(HttpStatus.CREATED);
}

POJO

POJO

package model;

import java.io.Serializable;

import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties;
import com.fasterxml.Hymanson.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 Client

REST客户端

try {
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
        list.add(new MappingHymanson2HttpMessageConverter());
        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<String> entity = new HttpEntity<String>(obj.toString(), headers);
        restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

        //restTemplate.postForEntity(url, entity, String.class);
    } catch(Exception ex) {
        ex.printStackTrace();
    }

As you can see in the commented, I have tried many options with no success.

正如您在评论中看到的那样,我尝试了很多选择,但都没有成功。

Stack Trace

堆栈跟踪

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)

EDITI had tested REST web-service http://localhost:8181/xyz/updateAdminusing Postman, and it is successfully executing, but not working with RestTemplate.

编辑我已经http://localhost:8181/xyz/updateAdmin使用 Postman测试了 REST web 服务,它成功执行,但不能与 RestTemplate 一起使用。

ISSUE SOLVEDSee my answer below.

问题已解决请参阅下面我的回答。

采纳答案by Jignesh M. Khatri

Just solved the issue by adding following code in dispatcher servlet configuration file of REST Client:

刚刚通过在 REST 客户端的调度程序 servlet 配置文件中添加以下代码解决了该问题:

<bean id="HymansonMessageConverter"
    class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="HymansonMessageConverter" />
        </list>
    </property>
</bean>

Full code of dispatcher servlet is: web-servlet.xml

dispatcher servlet 的完整代码为: web-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="webcontroller" />
<mvc:annotation-driven />
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name = "prefix" value = "/WEB-INF/jsp/" />
  <property name = "suffix" value = ".jsp" />
</bean>

<bean id="HymansonMessageConverter"
    class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="HymansonMessageConverter" />
        </list>
    </property>
</bean>

</beans>

Also removed some code from REST calling function, below are the changes: REST CLIENT

还从 REST 调用函数中删除了一些代码,以下是更改: REST CLIENT

try {
        RestTemplate restTemplate = new RestTemplate();
        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.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
        restTemplate.exchange(url, HttpMethod.POST, entity, String.class);


    } catch(Exception ex) {
        ex.printStackTrace();
    }

回答by KompiKompi

Have you tried without '/' in value?

你试过没有 '/' 的值吗?

@RequestMapping(value="updateAdmin", method=RequestMethod.POST, 
consumes="application/json", headers="Accept=application/json")
public ResponseEntity<String> updateAdmin(@RequestBody Ss_admin admin) 
{
    try {
        ss_admin_dao.updateAdmin(admin);
    } catch(Exception ex) {
    ex.printStackTrace();
    }
    return new ResponseEntity<String>(HttpStatus.CREATED);
}