SpringBoot相关的知识点总结

来源:互联网 发布:硬笔书法字帖推荐 知乎 编辑:程序博客网 时间:2024/05/29 19:34

1,为SpringBoot的Controller类写测试类,如下:

下面针对该Controller编写测试用例验证正确性,具体如下。当然也可以通过浏览器插件等进行请求提交验证:
package com.kfit.demo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.kfit.controller.UserController;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes=MockServletContext.class)//MockServletContext.class
@WebAppConfiguration
public class UserControllerTest extends MockMvcResultMatchers{
//模拟mvc对象类.
private MockMvc mvc;
@Before
public void setup(){
/*
* MockMvcBuilders使用构建MockMvc对象.
*/
mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
}
@Test
public void testUserController() throws Exception{
RequestBuilder request = null;
//1. get 以下user列表,应该为空》
//1、构建一个get请求.
request = MockMvcRequestBuilders.get("/users");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string("[]"))
;
System.out.println("UserControllerTest.testUserController().get");
// 2、post提交一个user
request = MockMvcRequestBuilders.post("/users")
.param("id","1")
.param("name","林峰")
.param("age","20")
;
mvc.perform(request).andExpect(status().isOk()).andExpect(content().string("success"));
// 3、get获取user列表,应该有刚才插入的数据
request = MockMvcRequestBuilders.get("/users");
mvc.perform(request).andExpect(status().isOk()).andExpect(content().string("[{\"id\":1,\"name\":\"林峰\",\"age\":20}]"));
// 4、put修改id为1的user
request = MockMvcRequestBuilders.put("/users/1")
.param("name", "林则徐")
.param("age", "30");
mvc.perform(request)
.andExpect(content().string("success"));
// 5、get一个id为1的user
request = MockMvcRequestBuilders.get("/users/1");
mvc.perform(request)
.andExpect(content().string("{\"id\":1,\"name\":\"林则徐\",\"age\":30}"));
// 6、del删除id为1的user
request = MockMvcRequestBuilders.delete("/users/1");
mvc.perform(request)
.andExpect(content().string("success"));
// 7、get查一下user列表,应该为空
request = MockMvcRequestBuilders.get("/users");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string("[]"));
}
}

2,使用@ConfigurationProperties注解进行编码

需要下面的依赖
<!--spring boot 配置处理器 -->      
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <optional>true</optional>
        </dependency>

配置类可以这样写:
@ConfigurationProperties(prefix="com.kfit.blog")
public class BlogProperties {
   
    private String name;//博客作者
   
    private String title;//博客标题
 
    // 省略gettersetter
}

在启动类中加入:
@EnableConfigurationProperties({BlogProperties.class})
这种的好处就是只需要配置一个地方,其它地方就是正常定义类的属性即可了。

3,
这条命令:java -jar xxx.jar --server.port=8888,通过使用--server.port属性来设置xxx.jar应用的端口为8888

4,
多环境的配置思路:
   application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置
   application-{profile}.properties中配置各个环境不同的内容
通过命令行方式去激活不同环境的配置。

执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为80,也就是生产环境的配置(prod

5,在springboot的开发中,开发包和测试包必须一一对应,否则build的时候会出错的.

6,在springboot 的入口类中,我们只需要打上:@SpringBootApplication即可.其他的就免了,比如:
 @Configuration  @EnableAutoConfiguration  @ComponentScan
因为前者是后面几个的汇总.

7,RESTful API
请求类型URL功能说明GET/users查询用户列表POST/users创建一个用户GET/users/id根据id查询一个用户PUT/users/id根据id更新一个用户DELETE/users/id根据id删除一个用户

8,代码提交命令
git pull origin features:features (前一个是远程仓库,后者是本地仓库)

git add .

git commit -m "updated some bugs"

git push origin features origin/features


9,

从前端传来日期的字符串,然后在项目中把字符串转化为Date类型,然后传入JPA的查询语句中.

顺便说下,在mysql中日期的比较可以直接使用 between and 进行.例如:

select * from `apps` where `created_at` between "2012-06-26 00:00:00" and "2012-06-26 00:00:00";
@Query("select mon from Monetization mon where appId=:appId and datetime between :startDate and :endDate group by dayid")


第一步:接收串
@GetMapping("/adrevenues")
publicResponseEntity listReveByAppidAndDate(@RequestParam("appId") String appId, @RequestParam("startDate")String startDate,@RequestParam("endDate")String endDate){
List<Monetization> monetizationList=monetizationService.findByAppidAndDate(appId,startDate,endDate);
List<RevenueDto> monetizationDtoList=Lists.newArrayList();
monetizationDtoList.addAll(monetizationList.stream().map(this::convertReven).collect(Collectors.toList()));
returnResponseEntity.ok(monetizationDtoList);
}

第二步:转换
@Override
publicList<Monetization> findByAppidAndDate(String appId, String startDate, String endDate){
Date start=null;
try{
start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDate);
Date end=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
returnmonetizationRepository.findByAppidAndDate(appId,start,end);
} catch(ParseException e) {
e.printStackTrace();
}
returnCollections.emptyList();
}

第三步:使用Date类型与mysql进行交互
public interfaceMonetizationRepository extends JpaRepository<Monetization,Long>{
@Query("select mon from Monetization mon where appId=:appId and datetime between :startDate and :endDate group by dayid")
publicList<Monetization> findByAppidAndDate(@Param("appId")String appId, @Param("startDate")Date startDate, @Param("endDate")Date endDate);

}

原创粉丝点击