0

I can do get on camel-example-swagger-cdi. But how do I do a put ?

I tried the following on browser, it didn't work at all. http://localhost:8080/user/{'id':'222','name':'Richard'}.

And I tried the following curl command, it didn't work either curl -X PUT -d id=222 -d name="Richard" localhost:8080/user

Please advise how I can issue a post

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");
    }

}

I used Postman to do a put:

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

Got Used Postman to do a put. Got

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

I got the following exception when I run curl to put.

$ 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)
johnsam
  • 4,192
  • 8
  • 39
  • 58

2 Answers2

1

If you can use GUI tools you should take a look at Postman, otherwise you can run:

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

This is how you do it in Postman

I would also recommend you to take a look at PUT vs POST in REST, and Swagger's Parameter Object docs.

Community
  • 1
  • 1
Henrique Viecili
  • 141
  • 1
  • 10
0

I figured it out. And it is working now. curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" -d "{ \"id\": 222,\"name\": \"Richard\"}" http://localhost:8080/user

johnsam
  • 4,192
  • 8
  • 39
  • 58