怎么做一个骆驼示例-swagger-cdi?

时间:2016-01-12 01:07:13

标签: rest apache-camel

我可以选择camel-example-swagger-cdi。但我怎么做一个put?

我在浏览器上尝试过以下操作,它根本不起作用。 http://localhost:8080/user/ {' ID':' 222''名称':'理查德'}

我尝试了以下curl命令,它也没有工作 curl -X PUT -d id = 222 -d name =" Richard"本地主机:8080 /用户

请告知我如何发帖

public class UserRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        // configure we want to use servlet as the component for the rest DSL
        // and we enable json binding mode
        restConfiguration().component("netty4-http").bindingMode(RestBindingMode.json)
            // and output using pretty print
            .dataFormatProperty("prettyPrint", "true")
            // setup context path and port number that netty will use
            .contextPath("/").port(8080)
            // add swagger api-doc out of the box
            .apiContextPath("/api-doc")
                .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
                // and enable CORS
                .apiProperty("cors", "true");

        // this user REST service is json only
        rest("/user").description("User rest service")
            .consumes("application/json").produces("application/json")

            .get("/{id}").description("Find user by id").outType(User.class)
                .param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
                .to("bean:userService?method=getUser(${header.id})")

            .put().description("Updates or create a user").type(User.class)
                .param().name("body").type(body).description("The user to update or create").endParam()
                .to("bean:userService?method=updateUser")

            .get("/findAll").description("Find all users").outTypeList(User.class)
                .to("bean:userService?method=listUsers");
    }

}

我用Postman做了一个put:

localhost:8080/user?body={'id':'222' , 'name':'Richard' }

GOT     二手Postman做了一个put。得到了

java.net.URISyntaxException: Illegal character in query at index 11: /user?body=
    {%27id%27:%272222%27%20,%20%27name%27:%27Richard%27%20}

当我运行curl来放置时,我得到以下异常。

$ curl -X PUT -d "body={'id':'222','name':'Richard'}" http://localhost:8080/user

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3142  100  3108  100    34  33419    365 --:--:-- --:--:-- --:--:-- 50129com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'body': was expecting ('true', 'false' or 'null')
 at [Source: java.io.ByteArrayInputStream@17b2613c; line: 1, column: 6]
        at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581)
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3451)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2610)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:841)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:737)
        at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3776)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3721)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2796)
        at org.apache.camel.component.jackson.JacksonDataFormat.unmarshal(JacksonDataFormat.java:173)
        at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69)
        at org.apache.camel.processor.binding.RestBindingProcessor.process(RestBindingProcessor.java:168)
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
        at org.apache.camel.component.netty4.handlers.ServerChannelHandler.processAsynchronously(ServerChannelHandler.java:134)
        at org.apache.camel.component.netty4.handlers.ServerChannelHandler.channelRead0(ServerChannelHandler.java:105)
        at org.apache.camel.component.netty4.http.handlers.HttpServerChannelHandler.channelRead0(HttpServerChannelHandler.java:211)
        at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
        at org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler.channelRead0(HttpServerMultiplexChannelHandler.java:113)
        at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:318)
        at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:42)
        at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:309)
        at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:36)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
        at java.lang.Thread.run(Thread.java:724)

2 个答案:

答案 0 :(得分:1)

如果你可以使用GUI工具,你应该看看Postman,否则你可以运行:

curl -X PUT -d "{'id':'222','name':'Richard'}" http://localhost:8080/user

This is how you do it in Postman

我还建议您查看PUT vs POST in RESTSwagger's Parameter Object文档。

答案 1 :(得分:0)

我明白了。它现在正在运作。 curl -X PUT --header" Content-Type:application / json" --header"接受:application / json" -d" {\" id \":222,\" name \":\" Richard \"}" http://localhost:8080/user