从 Java 8 迁移到 Java 17-XML 数据,在 Controller JDK 17 Spring 3.0.5 中从 postman 的 requestBody 中作为 NULL 接收

Migration from Java 8 to Java 17-XML data passed in requestBody from postman received as NULL in Controller JDK 17, Spring 3.0.5

提问人:Tech_Mommy 提问时间:8/3/2023 更新时间:8/3/2023 访问量:289

问:

'从 Java 8 迁移到 Java 17,Spring 3.0.5 面临问题。 当我从邮递员发送 XML 数据时,在控制器中获取 Null,而不是传递的实际值。 我正在使用 JDK 17 + Spring 版本 - 3.0.5。 标头 Content-Type - application/xml。io.swagger.v3.oas.annotations.parameters.RequestBody;

宝卓

import jakarta.validation.constraints.NotNull; 
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="HEADERMLW")
public class Headmlw implements Serializable  
{
private String routeId;
@NotNull(message = "RouteId cannot be empty ")
@XmlElement(name = "RouteId")
public String getRouteId() {
  return routeId;
 }
public void setRouteId(String routeId) {
 this.routeId = routeId;
  }
}`

控制器

import org.springframework.beans.factory.annotation.Autowired;    
import org.springframework.beans.factory.annotation.Value;    
import org.springframework.http.HttpStatus;   
import org.springframework.http.MediaType;   
import org.springframework.http.ResponseEntity;   
import org.springframework.util.CollectionUtils;   
import org.springframework.validation.BindingResult;    
import org.springframework.web.bind.annotation.PostMapping;    
import org.springframework.web.bind.annotation.RequestBody;    
import org.springframework.web.bind.annotation.RequestMapping;    
import org.springframework.web.bind.annotation.RestController;   
import jakarta.servlet.http.HttpServletResponse;   
import javax.validation.Valid;   
import lombok.extern.slf4j.Slf4j;

@Slf4j    
@RequestMapping(value = "/")    
public class MyControllerClass { 

@PostMapping(path = "/path/", consumes = MediaType. APPLICATION_XML_VALUE , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)    
public Mono\<List\<ResponseEntity\<ResponseModelData\>\>\> createPlanEvent( final @Valid  @RequestBody(required = true) Headmlw mlwRequest, BindingResult bindingResult)
{    
 `your text`//content goes here
 }    
}

使用以下依赖项

    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>4.0.3</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.2</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>javax.activation-api</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>jakarta.xml.ws</groupId>
        <artifactId>jakarta.xml.ws-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>`

如果我使用import org.springframework.web.bind.annotation.RequestBody;
它给出了以下错误响应,尽管我在 requestbody 中发送 xml 数据

{     "type": "about:blank", 
      "title": "Bad Request", 
      "status": 400,  
      "detail": "Invalid request content."
  }
xml 解析 jaxb spring-webflux jackson-databind java-17

评论

0赞 amanin 8/3/2023
您的 pom.xml 中有冲突的 jaxb 版本。您应该删除jaxb_impl,并删除与 jakarta 相关的工件的版本(甚至是 glassfish jaxb 运行时)。春天会提供它们。

答: 暂无答案