摇摆变形器+复杂的物体

时间:2017-12-13 10:09:01

标签: java json controller swagger

我正在尝试使用swagger inflector构建服务。 但是当我试图将复杂的对象发布到我的swagger控制器时,遇到了一些问题。请看下面提供的配置。

我的swagger.yaml:

swagger: "2.0"
info:
  version: 1.0.0
  title: Dummy Controller
basePath: /v1
tags:
  - name: Dummy Controller
    description: Dummy controller
schemes:
  - http
paths:
  /someService/create:
    post:
      tags:
        - someService
      summary: Test
      description: ""
      operationId: createPoint
      consumes:
            - application/json
            - application/xml
      produces:
        - application/xml
        - application/json
      parameters:
        - in: body
          name: body
          description: Created object
          required: false
          schema:
            $ref: '#/definitions/Point'
      responses:
        default:
          description: successful operation

definitions:
    Point:
    title: Point
    description: ''
    type: object
    properties:
      latitude:
        description: ''
        type: string
      longitude:
        description: ''
        type: string
    required:
    - latitude
    - longitude
  ApiResponse:
    properties:
      code:
        type: integer
        format: int32
      type:
        type: string
      message:
        type: string
    xml:
      name: "##default"

我的控制器:

public class SomeServiceController {

  public ResponseContext createPoint(RequestContext request, Point point){
    System.out.println(point);
    return new ResponseContext()
            .status(Status.OK);
  }
}

当我试图像这样发帖时:

POST http://localhost:8080/v1/someService/create
{
"latitude":"10",
"longitude":"20"
}

我得到了

11:36:51.814 [qtp931940545-13] ERROR i.s.i.c.SwaggerOperationController - failed to invoke method public io.swagger.inflector.models.ResponseContext com.swagger.test.SomeServiceController.createPoint(io.swagger.inflector.models.RequestContext,com.swagger.test.model.Point)
java.lang.IllegalArgumentException: argument type mismatch

我的依赖项:

<dependencies>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-inflector</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.1.1</version>
    </dependency>
</dependencies>

请帮忙!我错过了什么?

1 个答案:

答案 0 :(得分:2)

发现了下一个问题: 我的模型对象(Point)被放在错误的包中。 如果inflector无法找到模型,它会将JsonNode传递给Controller方法。 这就是为什么我收到了争论类型不匹配的原因。 Java无法将JsonNode映射到Point。

希望它对某人有所帮助!