【问题标题】:Spring boot RestTemplate post giving 400 errorSpring boot RestTemplate 发布 400 错误
【发布时间】:2019-05-19 22:45:31
【问题描述】:

我在 Eclipse Photon 上用 Java 8 编写了一个简单的 webService,使用 RestTemplate 发布(使用 postForObject)一个包装对象列表(称为 PatentDetails)的对象(称为patentListWrapper)。我从一个 Java 客户端(称为 MainWsClient )发布,然后在服务器端的 PatentDetails 中设置一个值,并在客户端读回 PatentListWrapper 对象。当服务器端(程序 SpringWebServiceHello)使用旧的 Spring MVC 4 技术时,它工作正常,只有 1 个 jar 文件(Spring-web.5.07.RELEASE.jar)跟随此 - serverSideExample 即 web.xml 和 rest-servlet.xml控制访问点的文件。然后我使用带有 Spring 5.07 jar 的 SpringBoot 2.03 和 Maven 编写了另一个服务器端程序 (PndGuidRequestWs),具有相同的 @RequestMapping 方法,但没有 web.xml 文件和在 application.properties 文件中定义的访问点:

server.port=8082
server.servlet.path=/
#spring.mvc.servlet.path=/
#server.servlet.contextPath=/

当我使用这个客户端调用这个新的服务器程序时 - ARC 它也可以正常工作 但是当我使用相同的 java 客户端和完全相同的请求调用它时(显然接受不同的 url)。我收到 400 错误:

2018-12-18 16:56:53,861 [main] INFO  - Running MainWsClient with name = DS fileType = post3
2018-12-18 16:56:54,101 [main] DEBUG - Created POST request for "http://localhost:8082/guidRequest/xmlList"
2018-12-18 16:56:54,145 [main] DEBUG - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
2018-12-18 16:56:54,152 [main] DEBUG - Writing [com.springservice.client.PatentListWrapper@4ba2ca36] using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@3444d69d]
2018-12-18 16:56:54,384 [main] DEBUG - POST request for "http://localhost:8082/guidRequest/xmlList" resulted in 400 (null); invoking error handler
2018-12-18 16:56:54,387 [main] ERROR - DS1B org.springframework.web.client.HttpClientErrorException: 400 null

不工作的,PndGuidRequestWs,服务器端有:

@RestController
public class PndController {

@RequestMapping(value = "/guidRequest/xmlList", method = RequestMethod.POST, produces = { "application/xml" } )
public PatentListWrapper guidSearchList(@RequestBody  PatentListWrapper patentListWrapper) {

    for (PatentDetails pd : patentListWrapper.getPatentList())
    {
        pd.setGuid("guidSetOnServer3");
    }

    return patentListWrapper;
  }

}

工作(SpringWebServiceHello)服务器端是一样的,除了:

value = "/service/greeting/xml/post2"

Java 客户端有:

public void runCode(String name , String fileType)
{

 String url;

 if (fileType.equalsIgnoreCase("post2")) {
        url = "http://localhost:8080/SpringWebServiceHello/service/greeting/xml/post2";
        // This method is identicle to postToPndGuidRequestWs() but this method works fine.
        postToSpringWebServiceHello(url);
    }else if (fileType.equalsIgnoreCase("post3")) {
        url = "http://localhost:8082/guidRequest/xmlList";      
        // This method gives 404 error          
        postToPndGuidRequestWs(url);
    }   
}

private void postToPndGuidRequestWs(String url) 
{

    PatentListWrapper patentListWrapper = new PatentListWrapper();
    PatentDetails pd = new PatentDetails("CN","108552082","A","00000000",12345,"guidIn");

    List<PatentDetails> patentList = new ArrayList<PatentDetails>();
    patentList.add(pd);
    patentListWrapper.setPatentList(patentList);

    RestTemplate restTemplate = new RestTemplate();

    /* HttpHeaders headers = new HttpHeaders();
    headers.add("header_name", "header_value");
    headers.setContentType(MediaType.APPLICATION_XML);
    HttpEntity<PatentListWrapper> request = new HttpEntity<PatentListWrapper>(patentListWrapper, headers); */

    /*List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(MediaType.APPLICATION_XML);
    jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
    messageConverters.add(jaxbMessageConverter);
    restTemplate.setMessageConverters(messageConverters);*/

    /* headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);*/


    try {
        patentListWrapper = restTemplate.postForObject(
                url,
                patentListWrapper,
                PatentListWrapper.class);


        logger.debug("DS1A employee obj returned. guid = " +  patentListWrapper.getPatentList().get(0).getGuid());
    }catch(Exception e) {
        logger.error("DS1B " + e);      
    }   
}

}

ie fileType="post2" 调用 SpringWebServiceHello,fileType="post3" 调用 PndGuidRequestWs。如您所见,我尝试了几种注释掉的解决方案,但没有任何效果。由于两个服务器端程序之间唯一真正的区别是没有一个工作程序使用 Spring boot,而工作程序没有问题必须在 SpringBoot 设置中,即目录结构、application.properties 或 pom.xml。我的 pom.xml 有:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.clarivate</groupId>
<artifactId>pndguidrequestws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>pndGuidRequestWs</name>
<description>Guid request webService</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <start-class>com.clarivate.pndguidrequestws.PndGuidRequestWsApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.1.0</version> 
      <!--    <scope>provided</scope> --> <!-- DS insert for unix -->
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Implementing XML Representation for Spring Boot Services -->
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>

    <!-- httpcomponents jars are Required by PndGuidGenerator -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
</dependencies>


<build>
    <finalName>PndGuidRequestWs</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId> 
            <configuration>
                  <executable>true</executable>
            </configuration> 
        </plugin>
    </plugins>      
</build>
</project>

PatentListWrapper 类是:

package com.clarivate.pndguidrequestws.model;

import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class PatentListWrapper {

private List<PatentDetails> patentList;

public PatentListWrapper() {}

public List<PatentDetails> getPatentList() {
    return patentList;
}

public void setPatentList(List<PatentDetails> patentList) {
    this.patentList = patentList;
}   

}

欢迎提出任何建议。

编辑: 为了简化对象,我只使用 1 个字符串成员创建了 PatentListWrapper2:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class PatentListWrapper2 {

private String name;

public  PatentListWrapper2() {}

public  PatentListWrapper2(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

我可以再次使用 ARC 客户端成功发送此 xml:

<patentListWrapper2>
   <name>DSDS</name>
</patentListWrapper2>

与 contentType="application/xml" 但是当我尝试从 java 发送patentListWrapper2 时,我得到一个解组错误:

 2018-12-20 09:17:13,931 [main] INFO  - Running MainWsClient with name = DS fileType = post4
2018-12-20 09:17:14,166 [main] DEBUG - Created POST request for "http://localhost:8082/guidRequest/xmlList2"
2018-12-20 09:17:14,200 [main] DEBUG - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
2018-12-20 09:17:14,206 [main] DEBUG - Writing [com.springservice.client.PatentListWrapper2@517cd4b] using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@6cc7b4de]
2018-12-20 09:17:14,246 [main] DEBUG - POST request for "http://localhost:8082/guidRequest/xmlList2" resulted in 200 (null)
2018-12-20 09:17:14,248 [main] DEBUG - Reading [com.springservice.client.PatentListWrapper2] as "application/xml;charset=UTF-8" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@6cc7b4de]
2018-12-20 09:17:14,255 [main] ERROR - DS2B org.springframework.web.client.RestClientException: Error while extracting response for type [class com.springservice.client.PatentListWrapper2] and content type [application/xml;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class com.springservice.client.PatentListWrapper2]: unexpected element (uri:"", local:"PatentListWrapper2"). Expected elements are <{}patentListWrapper2>; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"PatentListWrapper2"). Expected elements are <{}patentListWrapper2>

EDIT2 我在 Eclipse Tomcat 上运行 pndGuidRequestWs,而不是 - Run As -> Spring Boot App。服务器日志如下:

2018-12-20 11:15:45.655  WARN 236 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.clarivate.pndguidrequestws.model.PatentDetails` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('CN'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.clarivate.pndguidrequestws.model.PatentDetails` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('CN') at [Source: (PushbackInputStream); line: 1, column: 98] (through reference chain: com.clarivate.pndguidrequestws.model.PatentListWrapper["patentList"]->java.util.ArrayList[0])         

【问题讨论】:

  • 抱歉,但是调查日志你得到的是“400”而不是“404”!? (400 表示“错误请求”...)
  • @xerx593 抱歉,这是一个错字。正如你所说,错误是 400,我已经更正了帖子
  • 由于端点生成application/xml 并且restTemplate 解析application/json 这里可能存在问题-您可以尝试使用String.class 而不是PatentListWrapper.class 发布吗?之后您可能必须手动解析 XML 字符串。见stackoverflow.com/questions/12184731/…String response = restTemplate.postForObject( url, patentListWrapper, String.class);
  • @xerx593 你是对的。我在 PatentListWrapper2 中更改了 \@XmlRootElement -> @XmlRootElement(name="PatentListWrapper2") 并且它起作用了。但是,当我在 PatentListWrapper 中进行相同的更改时,它没有任何区别,仍然是 400 错误:(
  • @xerx593 我已经做到了,并使用 EDIT2 更新了帖子。为什么我在使用 xml 解析时出现 JSON 解析器错误?

标签: java spring rest spring-mvc spring-boot


【解决方案1】:

你能测试一下吗:

try {
        HttpHeaders headers = new HttpHeaders();
        //headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        //headers.setContentType((MediaType.APPLICATION_JSON));
        // I comment the code abouve because you did not specify a consumes whitch  defines the media types that the methods of 
        //a resource class or MessageBodyReader can accept. If not specified, a container will assume that any media type is acceptable.
        HttpEntity<PatentListWrapper> request = new HttpEntity<>(patentListWrapper, headers);
        PatentListWrapper patentListWrapperResult =  = restTemplate.exchange(url, HttpMethod.POST, request,PatentListWrapper.class);


        logger.debug("DS1A employee obj returned. guid = " +  patentListWrapper.getPatentList().get(0).getGuid());
    }catch(Exception e) {
        logger.error("DS1B " + e);      
    } 

【讨论】:

  • 谢谢,我尝试了您的建议,但仍然出现 400 错误。注意我必须使用: ResponseEntity PatentListWrapperResult = ...
  • 好的,您说使用 rest 客户端它可以工作,但不能使用 RestTemplate?你服务的网址是localhost:8082/guidRequest/xmlList
  • 我想如果你检查你的休息服务的堆栈,你会发现一个错误,比如:Cannot deserialize instance of com.clarivate.pndguidrequestws.model.PatentListWrapper out of START_ARRAY token !!
  • 堆栈跟踪在帖子中,您是说我可以为其余服务获取不同的堆栈跟踪,如果可以,我该怎么做。请注意,我已经在最后用 EDIT 更新了帖子。
【解决方案2】:

PatentListWrapper 是一个复杂的对象,不是一段 xml ,所以答案是去掉所有对 xml 的引用 ie

  1. 从 PatentListWrapper 中删除 @XmlRootElement(name="PatentListWrapper")。
  2. 将 jackson-*.jar(s) 添加到类路径中以进行消息转换
  3. 将服务器端@RequestMapping 更改为:

    @RequestMapping(value = "/xmlList", method = RequestMethod.POST , consumes = { "application/xml" }, produces = { "application/xml" }) 
    

@RequestMapping(value = "/xmlList", method = RequestMethod.POST )

这意味着 ARC 客户端现在返回 JSON(因为它是默认返回类型),即使我发送 xml,但这并不重要,因为它只是一个测试工具。

因此,在 Spring 2 中使用 RestTemplate 发布对象时,客户端不需要 contentType 设置或额外的 messageConverters,只需:

RestTemplate restTemplate = new RestTemplate();
MyObject myObjectReturn = restTemplate.postForObject(url,myObject,MyObject.class);

在服务器端:

@RestController
@RequestMapping(value = "/endPoint", method = RequestMethod.POST)
public MyObject anyMethodName(@RequestBody  MyObject myObject) {
     //Do stuff to myObject
     return myObject;
}

【讨论】:

    猜你喜欢
    • 2019-03-20
    • 2023-02-24
    • 2015-04-26
    • 2017-10-30
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    相关资源
    最近更新 更多