第3章 springboot接口返回json SpringBoot构造并返回一个json对象

数据的使用主要还是以JSON为主,我们不会去使用XML。

这个时候我们先不使用@RestController,我们使用之前SpringMVC的那种方式,就是@Controller。

 @ResponseBody就表示返回出去的数据是以一个JSON字符串或者JSON对象为主。

 /imooc-springboot-starter/src/main/java/com/imooc/controller/UserController.java

package com.imooc.controller;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imooc.pojo.User;

@Controller
public class UserController {
    
    @RequestMapping("/hello")
    @ResponseBody
    public User hello() {
        
        User u = new User();
        u.setName("imooc");
        u.setAge(18);
        u.setBirthday(new Date());
        u.setPassword("imooc");
        //u.setDesc(null);
        
        
        return u;
        
    }
}

 运行并启动项目,控制台打印了错误

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-10-27 10:41:22.306  INFO 10376 --- [           main] com.imooc.ImoocApplication               : Starting ImoocApplication on DESKTOP-S07DGSI with PID 10376 (D:\imooc-springboot-starter\imooc-springboot-starter\target\classes started by ZHONGZHENHUA in D:\imooc-springboot-starter\imooc-springboot-starter)
2018-10-27 10:41:22.314  INFO 10376 --- [           main] com.imooc.ImoocApplication               : No active profile set, falling back to default profiles: default
2018-10-27 10:41:22.488  INFO 10376 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1e67a849: startup date [Sat Oct 27 10:41:22 CST 2018]; root of context hierarchy
2018-10-27 10:41:26.674  INFO 10376 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-10-27 10:41:26.799  INFO 10376 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-10-27 10:41:26.800  INFO 10376 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-10-27 10:41:26.832  INFO 10376 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_144\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_144/bin/server;C:/Program Files/Java/jre1.8.0_144/bin;C:/Program Files/Java/jre1.8.0_144/lib/amd64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\libnvvp;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\BaiduNetdiskDownload\adt-bundle-windows-x86_64_20140101\adt-bundle-windows-x86_64_20140101\sdk\platform-tools;D:\BaiduNetdiskDownload\adt-bundle-windows-x86_64_20140101\adt-bundle-windows-x86_64_20140101\sdk\platform-tools;C:\Program Files\Java\jdk1.8.0_144\bin;C:\Program Files\Java\jdk1.8.0_144\jre\bin;I:\数据库\数据库3\mysql-5.7.21-winx64\mysql-5.7.21-winx64\bin;H:\NDK\android-ndk-r9d-windows-x86_64\android-ndk-r9d;D:\Users\ZHONGZHENHUA\Anaconda3\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Users\ZHONGZHENHUA\Server\maven\apache-maven-3.5.2\bin;C:\Program Files\TortoiseGit\bin;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA GPU Computing Toolkit\cudnn\bin;D:\Users\ZHONGZHENHUA\Anaconda3\Scripts;C:\Program Files\NVIDIA GPU Computing Toolkit\cudnn\bin;C:\ProgramData\NVIDIA GPU Computing Toolkit\v9.0;C:\Users\ZHONGZHENHUA\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\ProgramData\NVIDIA GPU Computing Toolkit\v8.0;C:\Users\ZHONGZHENHUA\AppData\Local\Microsoft\WindowsApps;C:\Users\ZHONGZHENHUA\AppData\Roaming\npm;D:\Wind\Wind.NET.Client\WindNET\bin\;G:\eclipse-jee-2018-09-win32-x86_64\eclipse;;.]
2018-10-27 10:41:27.162  INFO 10376 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-10-27 10:41:27.166  INFO 10376 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4683 ms
2018-10-27 10:41:27.447  INFO 10376 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-10-27 10:41:27.457  INFO 10376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-10-27 10:41:27.459  INFO 10376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-10-27 10:41:27.460  INFO 10376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-10-27 10:41:27.460  INFO 10376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-10-27 10:41:27.770  INFO 10376 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-27 10:41:28.339  INFO 10376 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1e67a849: startup date [Sat Oct 27 10:41:22 CST 2018]; root of context hierarchy
2018-10-27 10:41:28.629  INFO 10376 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.Object com.imooc.controller.HelloController.hello()
2018-10-27 10:41:28.634  WARN 10376 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'userController' method 
public com.imooc.pojo.User com.imooc.controller.UserController.hello()
to {[/hello]}: There is already 'helloController' bean method
public java.lang.Object com.imooc.controller.HelloController.hello() mapped.
2018-10-27 10:41:28.650  INFO 10376 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-10-27 10:41:28.690  INFO 10376 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-27 10:41:28.721 ERROR 10376 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'userController' method 
public com.imooc.pojo.User com.imooc.controller.UserController.hello()
to {[/hello]}: There is already 'helloController' bean method
public java.lang.Object com.imooc.controller.HelloController.hello() mapped.
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548) ~[spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
	at com.imooc.ImoocApplication.main(ImoocApplication.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'userController' method 
public com.imooc.pojo.User com.imooc.controller.UserController.hello()
to {[/hello]}: There is already 'helloController' bean method
public java.lang.Object com.imooc.controller.HelloController.hello() mapped.
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:581) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:545) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:267) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:252) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at java.util.LinkedHashMap.forEach(Unknown Source) ~[na:1.8.0_144]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:219) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:189) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:136) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1753) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1690) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	... 16 common frames omitted

修改UserController的@RequestMapping("/hello")为@RequestMapping("/user")

package com.imooc.controller;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imooc.pojo.User;

@Controller
public class UserController {
    
    //@RequestMapping("/hello")
    @RequestMapping("/user")
    @ResponseBody
    public User hello() {
        
        User u = new User();
        u.setName("imooc");
        u.setAge(18);
        u.setBirthday(new Date());
        u.setPassword("imooc");
        //u.setDesc(null);
        
        
        return u;
        
    }
}

运行并启动项目,控制台没打印错误了

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-10-27 11:05:06.338  INFO 5040 --- [           main] com.imooc.ImoocApplication               : Starting ImoocApplication on DESKTOP-S07DGSI with PID 5040 (D:\imooc-springboot-starter\imooc-springboot-starter\target\classes started by ZHONGZHENHUA in D:\imooc-springboot-starter\imooc-springboot-starter)
2018-10-27 11:05:06.345  INFO 5040 --- [           main] com.imooc.ImoocApplication               : No active profile set, falling back to default profiles: default
2018-10-27 11:05:06.611  INFO 5040 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3bbc39f8: startup date [Sat Oct 27 11:05:06 CST 2018]; root of context hierarchy
2018-10-27 11:05:11.911  INFO 5040 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-10-27 11:05:12.001  INFO 5040 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-10-27 11:05:12.002  INFO 5040 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-10-27 11:05:12.035  INFO 5040 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_144\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_144/bin/server;C:/Program Files/Java/jre1.8.0_144/bin;C:/Program Files/Java/jre1.8.0_144/lib/amd64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\libnvvp;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\libnvvp;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\BaiduNetdiskDownload\adt-bundle-windows-x86_64_20140101\adt-bundle-windows-x86_64_20140101\sdk\platform-tools;D:\BaiduNetdiskDownload\adt-bundle-windows-x86_64_20140101\adt-bundle-windows-x86_64_20140101\sdk\platform-tools;C:\Program Files\Java\jdk1.8.0_144\bin;C:\Program Files\Java\jdk1.8.0_144\jre\bin;I:\数据库\数据库3\mysql-5.7.21-winx64\mysql-5.7.21-winx64\bin;H:\NDK\android-ndk-r9d-windows-x86_64\android-ndk-r9d;D:\Users\ZHONGZHENHUA\Anaconda3\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Users\ZHONGZHENHUA\Server\maven\apache-maven-3.5.2\bin;C:\Program Files\TortoiseGit\bin;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA GPU Computing Toolkit\cudnn\bin;D:\Users\ZHONGZHENHUA\Anaconda3\Scripts;C:\Program Files\NVIDIA GPU Computing Toolkit\cudnn\bin;C:\ProgramData\NVIDIA GPU Computing Toolkit\v9.0;C:\Users\ZHONGZHENHUA\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\ProgramData\NVIDIA GPU Computing Toolkit\v8.0;C:\Users\ZHONGZHENHUA\AppData\Local\Microsoft\WindowsApps;C:\Users\ZHONGZHENHUA\AppData\Roaming\npm;D:\Wind\Wind.NET.Client\WindNET\bin\;G:\eclipse-jee-2018-09-win32-x86_64\eclipse;;.]
2018-10-27 11:05:12.592  INFO 5040 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-10-27 11:05:12.596  INFO 5040 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5991 ms
2018-10-27 11:05:12.932  INFO 5040 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-10-27 11:05:12.942  INFO 5040 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-10-27 11:05:12.942  INFO 5040 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-10-27 11:05:12.943  INFO 5040 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-10-27 11:05:12.945  INFO 5040 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-10-27 11:05:13.306  INFO 5040 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-27 11:05:13.776  INFO 5040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3bbc39f8: startup date [Sat Oct 27 11:05:06 CST 2018]; root of context hierarchy
2018-10-27 11:05:14.229  INFO 5040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.Object com.imooc.controller.HelloController.hello()
2018-10-27 11:05:14.239  INFO 5040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user]}" onto public com.imooc.pojo.User com.imooc.controller.UserController.hello()
2018-10-27 11:05:14.251  INFO 5040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-27 11:05:14.259  INFO 5040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-10-27 11:05:14.354  INFO 5040 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-27 11:05:14.354  INFO 5040 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-27 11:05:14.926  INFO 5040 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-27 11:05:15.103  INFO 5040 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-27 11:05:15.112  INFO 5040 --- [           main] com.imooc.ImoocApplication               : Started ImoocApplication in 9.782 seconds (JVM running for 10.974)

 再次修改UserController,重新运行并启动项目

package com.imooc.controller;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.imooc.pojo.User;

@Controller
@RequestMapping("/user")
public class UserController {
    
    //@RequestMapping("/hello")
    @RequestMapping("/getUser")
    @ResponseBody
    public User hello() {
      //public User getUser() {    
        User u = new User();
        u.setName("imooc");
        u.setAge(18);
        u.setBirthday(new Date());
        u.setPassword("imooc");
        //u.setDesc(null);
        
        
        return u;
        
    }
}

 这个就是一个User对象。在真正的开发中,我们日常出去一个JSON的数据,我们不会很简单地把一个这样子的一个类型数据给抛出去,我们肯定是要对它做一个额外的封装。自己构建一个封装类/imooc-springboot-starter/src/main/java/com/imooc/pojo/LeeJSONResult.java

猜你喜欢

转载自www.cnblogs.com/ZHONGZHENHUA/p/9859920.html