Java 调试 RestTemplate Post 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21402950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:30:04  来源:igfitidea点击:

Debug RestTemplate Post request

javajsonspringrestresttemplate

提问by Learn More

I am trying to POSTto a REST-APIusing RestTemplatein Spring. Here is the code I am using:

我想POST以一个REST-API使用RestTemplateSpring。这是我正在使用的代码:

//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.

我需要正确调试它并查看restTemplate.postForEntity(putUserUrl, submitRequestData, String.class);逐行发送到 REST 服务器的确切 JSON 是什么。

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

我正在使用Eclipse. 我试过逐行调试代码。我也尝试将日志级别设置为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.

下面是我的log4j.xml,我看不到任何与 REST 模板相关的 http 日志。如果我做错了什么,请告诉我。

<?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.

我打算打印从 POJO 创建的 JSON,然后再将其发送到 REST POST URL。

采纳答案by Jakub Kubrynski

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

您可以AbstractHttpMessageConverterpublic final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage)方法的末尾设置断点,并评估outputMessage.getBody()

回答by Alex K.

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

AFAIK,通常设置日志级别会有所帮助,但您可能遇到此处描述的类似情况(请参阅未接受的答案)或此处