2015-11-26 24 views
0

我们正在使用扬鞭写我们的REST API。我们有一个POST服务,它应该接受请求主体中的XML文件。 这是我们的请求定义:如何编写招摇API接受的XML请求主体

/services/invoke: 
    post: 
     tags: 
     - invoke 
     summary: A request invocation 
     operationId: invokeUsingPOST 
     consumes: 
     - application/xml 
     produces: 
     - application/xml 
     parameters: 
     - name: User-Token 
      in: header 
      description: The user token 
      required: false 
      type: string 
     - in: body 
      name: request 
      description: invoke request XML 
      required: false 
      schema: 
      type: string 
     responses: 
     '200': 
      description: OK 
      schema: 
      type: string 
     '400': 
      description: Bad Operation 
     '401': 
      description: Unauthorized 
     '404': 
      description: Forbidden 

然而,当我们使用招摇,代码生成生成Java客户端代码,生成的方法是这样的:

public String invokeUsingPOST (String userToken, Request request) 

和请求类的产生:

@ApiModel(description = "") 
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-11-25T18:45:31.524+02:00") 
public class Request { 
    @Override 
    public String toString() { 
    StringBuilder sb = new StringBuilder(); 
    sb.append("class Request {\n"); 
    sb.append("}"); 
    return sb.toString(); 
    } 
} 

如何使用它发送我的XML?我必须从它派生并重写toString()方法,还是有更好的方法来做到这一点?

回答

1

有一个错误,这是约2小时前解决。请从Swagger-Codegen中获取最新信息,并且Java API客户端中的参数request应该是字符串而不是模型。

+0

感谢。你能否提一下这个问题或解决方法? 无论如何,这有助于解决问题,但不能解决问题,因为调用失败是因为它调用serialize(),期望其有效内容为JSON,如果不是,则会引发异常。见https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache#L386 – splintor

+0

我得到这个异常: 产生的原因: io.swagger.client.ApiException:无法将对象序列化为Content-Type:application/xml at io.swagger.client.ApiClient.serialize(ApiClient.java:386) at io.swagger.client.ApiClient.getAPIResponse( ApiClient.java:493) 在io.swagger.client.ApiClient.invokeAPI(ApiClient.java:540) – splintor

+0

请https://github.com/swagger-api/swagger-codegen/issues/new打开一个问题,我们会研究这一点。 –

0

我快速的解决方法是,如果再添​​别的成序列化方法来检查,如果内容类型是“应用程序/ XML”

} else if (contentType.equals("application/xml")) { 
      SerializerUtils s = new SerializerUtils(); 
      return s.serializeRequestBody(contentType, obj); 
} 

和SerializerUtils有这2种方法

public class SerializerUtils { 

public Marshaller registerSerializer(Class<?> modelClass) throws JAXBException { 
    JAXBContext jaxbContext = JAXBContext.newInstance(modelClass); 

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
    return jaxbMarshaller; 
} 

public RequestBody serializeRequestBody(String contentType, Object obj) { 
    StringWriter sw = new StringWriter(); 
    try { 
     registerSerializer(obj.getClass()).marshal(obj, sw); 

    } catch (JAXBException e) { 
     e.printStackTrace(); 
    } 
    return RequestBody.create(MediaType.parse(contentType), sw.toString()); 
} 

这个工程对我来说,我希望它有助于

+0

并使用@XmlRootElement注释您的模型类 –