var_dump ();
or the debugging functions provided by each framework, but it is difficult to do in Java.System.out.println ("");
, but is it because the existing system settings are bad? , I couldn't run the app from the integrated development environment even after working for several hours.var_dump ();
that passes objects roughly and displays all the contents. Since only character strings can be output, some ingenuity is required for confirmation output.package package name;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
// @By specifying RestController, you can control the output only with this controller without using the View file.
@RestController
public class DummyController {
private static final Logger logger = LoggerFactory.getLogger(DummyController.class);
/**
*Write the content you want to check in the test code, add the output to the list of strings, and display them on the screen.
* URL :Site domain/dummy
* produces = "text/plain;charset=UTF-8"By specifying, the output will be treated as text instead of HTML, and Japanese character strings can also be used.
*/
@RequestMapping(value = {"/dummy"}, produces = "text/plain;charset=UTF-8")
public String dummy() {
return this.printToScreen(this.buildListToConfirm());
}
/**
*Test code you want to check the output
*/
private List<String> codeTrial(List<String> list) {
/*-----------------------------------*/
//Test code from here
/*-----------------------------------*/
//Write the code you want to check here
//Add the character string you want to output to the screen to list
list.add("hoge");
/*-----------------------------------*/
//Test code up to here
/*-----------------------------------*/
return list;
}
private List<String> buildListToConfirm(){
List<String> list = new ArrayList<String>();
try {
list.add("----------start----------");
list = this.codeTrial(list);
list.add("----------End----------");
} catch (Exception e) {
list.add(this.getStackTraceStr(e));
}
return list;
}
/**
*Show stack trace of errors
*/
private String getStackTraceStr(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
String stackTraceStr = sw.toString();
return stackTraceStr;
}
/**
*Display on screen for confirmation
*/
private String printToScreen(List<String> list){
String output = "";
for(String s : list){
output = output + s + "\n";
}
return output;
}
}
After writing the code, build it and access the URL of "/ dummy". The URL can be changed.
A stack trace is output when exiting with an error
Recommended Posts