【问题标题】:JAXBException occurred : unexpected element while calling Rest Service from Postman clientJAXBException 发生:从 Postman 客户端调用 Rest Service 时出现意外元素
【发布时间】:2015-09-16 07:03:48
【问题描述】:

我有一个使用 Spring 和 CXF 编写的休息服务。方法如下图。

@POST
@Path("/detail")
public StudentResponse getStudentDetails(Student student);

学生班。

@XmlRootElement(name="Student")
public class Student implements Serializable{
...

我可以使用 cxf 客户端调用服务

WebClient client = WebClient.create("http://localhost:8180/oauthserver/service/student/detail");
            client.type("application/json").accept("application/json");
            Response response =client.post(s);

            StudentResponse sr = response.readEntity(StudentResponse.class);

我发布的数据(使用jackson生成json条目)

  {
      "name" : "input",
      "id" : 1,
      "marks" : 20.2
    }

但在从 PostMan 为 SoapUI 调用服务时,我收到以下错误。

        JAXBException occurred : unexpected element (uri:"", local:"name"). 
    Expected elements are <{}Student>.
 unexpected element (uri:"", local:"name"). Expected elements are <{}Student>.

我以 POST 方法和上下文类型 RAW 调用。我已将标头值设置为:

Content-Type : application/json
Accept : application/json

但它仍然无法正常工作。有什么指点吗?

【问题讨论】:

  • 如果你使用{ "Student" : { .... } }会发生什么。顺便说一句,我不认为杰克逊被利用了。我想杰克逊不应该有关于 JAXBExceptions 的错误
  • 谢谢。在前面添加学生解决了这个问题。

标签: java json spring rest cxf


【解决方案1】:

仅为遇到相同问题的任何人发布此内容。

我必须将杰克逊提供者添加到服务列表中

<jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
        </jaxrs:providers>

然后需要添加maven依赖(新版本会有不同的包依赖)

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

此页面有解决方案... http://cxf.apache.org/docs/jax-rs-data-bindings.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2022-11-22
    • 2017-09-13
    相关资源
    最近更新 更多