src/main/resources/templates/test1/index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8" /> </head> <body> <p id="p1"></p> <p id="p2"></p> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $("#p1").text(""); $("#p2").text(""); var json1 = { bangou: "1", name: "鈴木", }; $.ajax({ url: "http://localhost:8080/test1/index", type: "POST", contentType: "application/json", data: JSON.stringfy(json1), dataType: "json", }) .done(function (data1, textStatus, jqXHR){ $("#p1").text(jqXHR.status); $("#p2").text(JSON.stringify(data1)); }) .fail(function (jqXHR, textStatus, errorThrown){ $("#p1").text(jqXHR.status); }) .always(function() {}); </script> </html>
Syain.java
package com.example.demo; import java.io.Serializable; public class Syain implements Serializable { private static final long serialVersionUID = 1L; private String bangou; private String name; public String getBangou() { return bangou; } public void setBangou(String bangou) { this.bangou = bangou; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
MainController.java
L @ResponseBodyはコントローラからの戻り値を返す
L
package com.example.demo; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test1") public class MainController { @PostMapping("/index") @ResponseBody public Syain output1( @RequestBody Syain syain) { System.out.println(syain.getBangou()); System.out.println(syain.getName()); return syain; } }
2021-02-07 12:07:42.608 INFO 44783 — [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 772 ms
2021-02-07 12:07:42.743 INFO 44783 — [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor’
2021-02-07 12:07:42.889 INFO 44783 — [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-02-07 12:07:42.919 INFO 44783 — [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ”
2021-02-07 12:07:42.927 INFO 44783 — [ restartedMain] com.example.demo.TestApplication : Started TestApplication in 1.355 seconds (JVM running for 7.09)
2021-02-07 12:08:35.401 INFO 44783 — [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet’
2021-02-07 12:08:35.401 INFO 44783 — [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet’
2021-02-07 12:08:35.402 INFO 44783 — [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2021-02-07 12:08:37.410 WARN 44783 — [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ not supported]
うーむ、上手く表示されんな。。