6

I am trying to POST to a REST-API using RestTemplate in Spring. Here is the code I am using:

//Code to Post data using Rest Template
List<UserVO> userList = getUsers();
RestRequestVO submitRequestData = new RestRequestVO();
submitRequestData.setAction("update");
submitRequestData.setType("user");
submitRequestData.setItems(items);
ResponseEntity<String> resposne = restTemplate.postForEntity(putUserUrl, submitRequestData, String.class);
String message = resposne.getBody();

//The structure of class
public class RestRequestVO {
private String action;
private String type;
private List<UserVO> items;

//Setters and Getters
}

//Expected JSON
{
"action"="update",
"type"="user",
"items"=[
    { //user1 }, {//user2} ....
]
}

I need to debug this properly and see what is the exact JSON being sent to the REST server by restTemplate.postForEntity(putUserUrl, submitRequestData, String.class); line.

I am using Eclipse. I have tried debugging code line by line. I have also tried setting log-level to Debug.

UPDATE after following steps given in comments

Below is my log4j.xml, I can not see any http logs related to REST Template. Please let me know if I have done some mistake.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss,SSS} - %5t - %5p - %l  - %m%n" />
        </layout>
    </appender>

    <appender name="FA" class="org.apache.log4j.FileAppender">
        <param name="File" value="cw.log"/>
        <param name="Threshold" value="DEBUG"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss,SSS} - %5t - %5p - %l  - %m%n" />
        </layout>
    </appender>

    <logger name="log4j.logger.httpclient.wire" additivity="false" > 
    <level value="DEBUG" /> 
    <appender-ref ref="CA"/> 
    </logger>

    <root>
        <level value="Debug" />
        <appender-ref ref="CA" />
    </root>

</log4j:configuration>

I intend to print JSON which is being created from POJO before it is sent to REST POST URL.

Learn More
  • 1,535
  • 4
  • 29
  • 51

2 Answers2

18

You can set breakpoint in AbstractHttpMessageConverter at the end of the public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage) method, and evaluate outputMessage.getBody()

Jakub Kubrynski
  • 13,724
  • 6
  • 60
  • 85
1

AFAIK, usually setting the log level helps, but may be you have the similar situation described here (see not accepted answer) or here.

Community
  • 1
  • 1
Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • Ultimatly, I would want the logging solution to work because that will help me analyse a process which is running on server(s). Accepted answer is helping me in only debugging process. – Learn More Jan 28 '14 at 15:21