how to see actual body sent from restassured

有些话、适合烂在心里 提交于 2020-06-27 08:17:30

问题


I have created a jax-rs rest api and tested it with rest-assured. All tests are green. Now I am trying to create an html/js front end for it.

My problem is I do not know how my json objects should look like to be accepted by my rest api. Thanks to restassured/jax-rs I never handled request strings. I fill in objects and I get objects, (un)marshaling (json) is invisible.

Is there any way I can see (debug) what strings are created by rest-assured/java and sent over the "wire"?


回答1:


If you want to log the request body you can do:

given().log().body(). ..

Or if you want to log the response body you can do:

.. .then().log().body(). ..

See documentation on logging for more details.




回答2:


I'm not a RestAssured used, so I can't answer your question directly, but here are some ideas that may help you out.

  • I don't know what serializer RestAssured uses under the hood, but Resteasy on Wildfly uses Jackson by default. I would get familiar with this library. For less trivial application, you may need to dig into its APIs directly to get your desired results. Here is it's documentation. For your particular case you can do something as simple as

    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(yourObject);
    System.out.println(jsonString);
    

    This will print out the POJO in JSON format, based on your getters in the class. This is at the most basic level. If you don't already have Jackson as a dependency, you can add

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.0</version>
    </dependency>
    
  • A really good friend (tool) to have is cURL. It's a command line tool that allows you to make REST/HTTP (other protocols also) requests. Though for this particular case it wouldn't help, you could send a GET request to one your resources that serves up the same type that you accepted in your POST. That way, you can see the resulting JSON. This may be a little much at this point, but I would definitely look into this tool if you're going to be doing a lot of REST development.

  • You might also want to check out a Browser tool like [Postman for Chrome]

  • You should really get familiar with JSON format. Once you get familiar with it, and you start working with JSON framework, you'll notice that at a basic level, they all work similarly.

    Java Object == JSON Object ( {} )
    Java Collection/Array == JSON Array ( [] )
    Java fields/properties == JSON keys
    
    Getters are used for they keys and their values (for serialization)
    Setters are used for deserialization
    

    So for example you have this class

    public class Person {
        String name;
        List<Person> friends;
    
        public String getName() { return name; }
        public void setName(String name) { return name; }
        // Getter and Setter for friends
    }
    

    An instance of Person would produce the following JSON

    {
        "name" : "Peeskillet",
        "friends": [
            {
                "name": "Lebron James"
            },
            {
                "name": "Steph Curry"
            }
        ]
    }
    

It's actually pretty simple once you get the hang of it.

  • Oh and another thing you can do is add a logging filter on the server side as mentioned here.

As far as working with Javascript, there is a JSON.stringify(javascriptObject) that will serialize your Javacript objects to JSON strings. So generally, you can model your Javascript object like your Java objects.

Hope this helped.




回答3:


In RestAssured just use this:

String body = resp.asString(); System.out.println(body);



来源:https://stackoverflow.com/questions/30644453/how-to-see-actual-body-sent-from-restassured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!