我试图在
Spring中使用RestTemplate POST到REST-API.
这是我正在使用的代码:
这是我正在使用的代码:
//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} .... ] }
我需要正确调试它,看看restTemplate.postForEntity(putUserUrl,String.class)发送到REST服务器的确切JSON是什么;线.
我正在使用Eclipse.我已经尝试逐行调试代码.
我也尝试将日志级别设置为Debug.
遵循注释中给出的步骤后更新
下面是我的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>
我打算在将POJ发送到REST POST URL之前打印从POJO创建的JSON.
解决方法
您可以在public final void write(最终T t,MediaType contentType,HttpOutputMessage outputMessage)方法结束时在AbstractHttpMessageConverter中设置断点,并评估outputMessage.getBody()