1.如何用springboot写helloworld
Helloworld使用传统的springmvc,需要配置web.xml,applicationContext.xml,然后打包为war在tomcat中运行,而如果使用springboot,一切都变得简单起来了。
下面使用Maven来创建springboot的webapp工程pom.xml4.0.0org.springframeworkgs-spring-boot0.1.0org.springframework.bootspring-boot-starter-parent1.3.3.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtest1.8org.springframework.bootspring-boot-maven-pluginHelloControllerpackagehello;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassHelloController{@RequestMapping("/")publicStringindex(){return"GreetingsfromSpringBoot!";}}其中:@RestController表示使用springmvc来接收request请求@RequestMapping映射到主页当请求返回的时候,是纯文本,那是因为@RestController是由@Controller和@ResponseBody组成Application@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[]args){ApplicationContextctx=SpringApplication.run(Application.class,args);System.out.println("Let'sinspectthebeansprovidedbySpringBoot:");}}其中:@SpringBootApplication代表了其有四个注解组成:@Configuration,@EnableAutoConfiguration,@EnableWebMvc,@ComponentScan在SpringApplication.run中会去自动启动tomcatrun方法返回上下文,在这个上下文中可以拿到所有的bean没有一行配置代码、也没有web.xml。基于SpringBoot的应用在大多数情况下都不需要我们去显式地声明各类配置,而是将最常用的默认配置作为约定,在不声明的情况下也能适应大多数的开发场景。
总体而言springboot是对javawebapp开发的简化单元测试@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes=MockServletContext.class)@WebAppConfigurationpublicclassHelloControllerTest{privateMockMvcmvc;@Beforepublicvoidbefore()throwsException{mvc=MockMvcBuilders.standaloneSetup(newHelloController()).build();}@Afterpublicvoidafter()throwsException{}/****Method:index()**/@TestpublicvoidtestIndex()throwsException{//TODO:Testgoesheremvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("GreetingsfromSpringBoot!")));}}建立restfullweb服务器接上,使用srpingboot建立web服务器就非常简单了,首先建立一个pojo类publicclassGreeting{privatefinallongid;privatefinalStringcontent;}然后使用control来handle] view plain copy org.springframework.boot spring-boot-configuration-processor true 2、配置文件(application.yml)中配置各个属性的值:[plain] view plain copy myprops: #自定义的属性和值 simpleprop: simplepropvalue arrayprops: 1,2,3,4,5 listprop1: - name: abc value: abcvalue - name: efg value: efgvalue listprop2: - config2value1 - config2vavlue2 mapprops: key1: value1 key2: value2 3、创建一个bean来接收配置信息:[java] view plain copy @component @configurationproperties(prefix="myprops") //接收application.yml中的myprops下面的属性 public class myprops { private string simpleprop; private string[] arrayprops; private list> listprop1 = new arraylist(); //接收prop1里面的属性值 private list listprop2 = new arraylist(); //接收prop2里面的属性值 private map mapprops = new hashmap(); //接收prop1里面的属性值 public string getsimpleprop() { return simpleprop; } //string类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要 public void setsimpleprop(string simpleprop) { this.simpleprop = simpleprop; } public list> getlistprop1() { return listprop1; } public list getlistprop2() { return listprop2; } public string[] getarrayprops() { return arrayprops; } public void setarrayprops(string[] arrayprops) { this.arrayprops = arrayprops; } public map getmapprops() { return mapprops; } public void setmapprops(map mapprops) { this.mapprops = mapprops; } } 启动后,这个bean里面的属性就会自动接收配置的值了。
4、单元测试用例:[java] view plain copy @autowired private myprops myprops; @test public void propstest() throws jsonprocessingexception { system.out.println("simpleprop: " + myprops.getsimpleprop()); system.out.println("arrayprops: " + objectmapper.writevalueasstring(myprops.getarrayprops())); system.out.println("listprop1: " + objectmapper.writevalueasstring(myprops.getlistprop1())); system.out.println("listprop2: " + objectmapper.writevalueasstring(myprops.getlistprop2())); system.out.println("mapprops: " + objectmapper.writevalueasstring(myprops.getmapprops())); } 测试结果:[plain] view plain copy simpleprop: simplepropvalue arrayprops: ["1","2","3","4","5"] listprop1: [{"name":"abc","value":"abcvalue"},{"name":"efg","value":"efgvalue"}] listprop2: ["config2value1","config2vavlue2"] mapprops: {"key1":"value1","key2":"value2"}。
4.SpringBoot搭建一个最简单的Hellworld怎么不能用呢
1. 修改非常简单
只需要将controller修改为@RestController就OK直接给你一个页面
2. 就是如果你想挑转到test1.jsp页面
2.1 properties里面添加
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
2.2. 在pom文件里面添加
<!-- //jasper是将jsp转化为jvm 能识别的class.java 文件 -->
<;!--由于Spring boot使用的内嵌的tomcat,而内嵌的tamcat是不支持jsp页面的,所有需要导入额外的包才能解决。 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
5.SpringBoot中如何分开写配置文件
案例:
1.application.properties
2.application-cus1.properties
3.application-cus1.properties
4.application-cus1.properties
5.controller
6.结果
转载请注明出处51数据库 » springbootword