如何解释Swagger / Jersey REST客户端的响应?

时间:2018-06-15 09:38:44

标签: java jersey swagger swagger-codegen

当我尝试使用swagger和jersey生成的rest客户端解释响应时包含:

  

应用/ XML;字符集= UTF-8

我得到以下异常:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/xml;charset=utf-8, type=class com.company.rest.client.model.generated.Result, genericType=class com.company.rest.client.model.generated.Result. at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:232)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1091)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:834)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:363)
    at org.glassfish.jersey.client.InboundJaxrsResponse$2.call(InboundJaxrsResponse.java:126)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:389)
    at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:264)

我尝试了不同的方式来定义消费,例如:

  • application / xml
  • 应用/ XML;字符集= UTF-8
  • 应用/ XML;字符集= UTF-8
  • application / xml,charset = utf-8

...

扬鞭-YAML:

swagger: '2.0'
info:
  description: REST API
  version: '1.0'
  title: REST API
  contact:
    email: support@company.com

host: setinproperties
basePath: /api/v1
schemes:
 - https
consumes:
  - application/xml;charset=UTF-8
produces:
  - application/xml

definitions:
  Result:
     type: object
     required:
       - DateTime  
       - Key
       - Value   
     properties:
      DateTime:
        type: string
        format: date-time
      Key:
        type: string
      Value:
        type: string

paths:
  '/result':
    get:
      summary: getResult
      operationId: getResult
      description: 'description'
      responses:
        200:
          description: success
          schema:
            $ref: '#/definitions/Result'
        400:
          description: bad request
          schema:
            $ref: '#/definitions/Result'
        500:
          description: internal server error
          schema:
            $ref: '#/definitions/Result'  

示例有效负载:

<?xml version="1.0" encoding="utf-8"?>
<Result>
 <DateTime>2018-06-19T07:50:21.8688563Z</DateTime>
 <Key>a key</Key>
 <Value>a value</Value>
</Result>

Pom.xml代码部分

  <plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>${swagger-codegen-maven-plugin.version}</version>
    <executions>
     <execution>
      <goals>
       <goal>generate</goal>
      </goals>
      <configuration>
       <inputSpec>${project.basedir}/src/main/resources/api.yml</inputSpec>
       <language>java</language>
       <library>jersey2</library> 
       <output>${project.build.directory}/generated-sources</output>
       <apiPackage>${default.package}.handler.generated</apiPackage>
       <modelPackage>${default.package}.model.generated</modelPackage>
       <invokerPackage>${default.package}.handler.generated</invokerPackage>
       <generateApiTests>false</generateApiTests>
       <generateApiDocumentation>false</generateApiDocumentation>
       <generateModelTests>false</generateModelTests>
       <configOptions>
        <sourceFolder>src/main/java</sourceFolder>
        <dateLibrary>joda</dateLibrary>
       </configOptions>
      </configuration>
     </execution>
    </executions>
   </plugin>

我怎样才能做到这一点?

使用Swagger 2.0版,swagger-code-gen版本2.3.1和Jersey lib版本2.27

0 个答案:

没有答案