[Java Spring MVC] Controller for development confirmation

Web application development in Java is difficult if you are accustomed to PHP

Make a dummy controller to help development

Dummy controller

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.

Screen output sample

スクリーンショット 2018-10-05 9.23.04.png

A stack trace is output when exiting with an error スクリーンショット 2018-10-05 9.35.54.png

Recommended Posts

[Java Spring MVC] Controller for development confirmation
Java Config with Spring MVC
Introducing Spring Boot2, a Java framework for web development (for beginners)
Spring Framework tools for Java developer
Build Java development environment (for Mac)
Spring Java
Sample code collection for Azure Java development
Spring Boot + Docker Java development environment construction
Transition from Struts2 to Spring MVC (Controller)
Convenient plugin for Eclipse JAVA development: Decompiler
Features of spring framework for java developers
Java development training
Test controller with Mock MVC in Spring Boot
For JAVA learning (2018-03-16-01)
[Java development] Java memory
Is Java SE8 Silver useful for Java development work? ??
Understanding the MVC framework with server-side Java 3/4 Controller
GAE / Java8 trial (Part 5: "Console for local development")
Java development environment
Request parameter log output sample Java & Spring MVC
2017 IDE for Java
Build a development environment for Docker, java, vscode
Java for statement
[Java] Spring DI ③
Things to watch out for in future Java development
Java development for beginners to start from 1-Vol.1-eclipse setup
[Java] Platforms to choose from for Java development starting now (2020)
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
[Java] for statement, while statement
Java development environment memo
[Java] Package for management
[Java] for statement / extended for statement
Countermeasures for Java OutOfMemoryError
NLP for Java (NLP4J) (2)
(Memo) Java for statement
NLP for Java (NLP4J) (1)
Java development link summary
java development environment construction
[Development] Java framework comparison
Web application development environment construction in Java (for inexperienced people)
Enable WebJars for blank projects in TERASOLUNA 5.x (= Spring MVC)
Eclipse installation and code completion enhancements (Mac for Java development)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
Build WebAPP development environment with Java + Spring with Visual Studio Code
[Java] Sample project for developing web applications with Spring Boot