zoukankan      html  css  js  c++  java
  • done 多米诺pizza oa 考了spring boot编程

     还考了oop编程,第一次在stackoverflow上贡献答案:https://stackoverflow.com/questions/52614766/user-moderator-and-admin-with-oop-javascript/53084174

    @service = @Component,表示东西
    https://www.tutorialspoint.com/spring_boot/spring_boot_service_components.htm
    controller service在一个类里面的话,就只定义一个class就行了
    测试文件的article不能在主文件中用,主文件自身也要有article

    package articles;
    
    import static org.hamcrest.Matchers.*;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.junit.BeforeClass;
    import org.junit.Before;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.http.MediaType;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class ApplicationTest {
        private static List<Article> articles = new ArrayList<Article>();
        private static ArticleService service = new ArticleService();
    
        @Autowired
        private MockMvc mockMvc;
      
        @BeforeClass 
        public static void populateArticles() {
            articles.add(new Article("10 things that you thought were unhealthy"));
            articles.add(new Article("You won't sleep until you read this"));
            articles.add(new Article("I ran out of catchy titles"));
        }
      
        @Before 
        public void clearDB() {
            this.service.clear();
        }
      
        public void addArticles() {
            for(Article article: articles) {
              this.service.add(article);
            }
        }
    
        @Test
        public void shouldRetrieveNothingFromEmptyDatabase() throws Exception {
            this.mockMvc.perform(get("/articles"))
                .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
                    .andExpect(jsonPath("$", hasSize(0)));
        }
      
        @Test
        public void shouldRetrievePostedArticles() throws Exception {
            addArticles();
            this.mockMvc.perform(get("/articles"))
                .andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8))
                    .andExpect(jsonPath("$", hasSize(articles.size())));
        }
      
        @Test
        public void shouldAllowUsToFindArticles() throws Exception {
            addArticles();
            Article article = this.service.getAll().get(0);
            this.mockMvc.perform(get("/articles/" + article.getId()))
                .andExpect(jsonPath("id", is(article.getId())))
                .andExpect(status().isOk());
        }
      
        public static String asJsonString(final Object obj) {
            try {
                final ObjectMapper mapper = new ObjectMapper();
                final String jsonContent = mapper.writeValueAsString(obj);
                return jsonContent;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }  
    }

     答案:

    package articles;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.http.HttpStatus;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        import org.springframework.web.server.ResponseStatusException;
    
        @RestController
        @RequestMapping("/articles")
        public class ArticlesController {
    
        private final ArticalService articalService;
    
        @Autowired
        public ArticlesController(ArticalService articalService){
            this.articalService = articalService;
        }
    
        @GetMapping
        public List<Article>getAll(){
           return articalService.getAll();
        }
        @GetMapping("/{id}")
        public Article findById(@PathVariable int id){
            Article article = articalService.findById(id);
    
            if(article != null){
                return article;
            }else{
                throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no such article with id:" + id);
            }
        }
    }

    总之controller就是个协调器,类都在它里面。model,viewer不用改,改controller就行了

  • 相关阅读:
    配置Robot Framework 环境时如何查看wxPython是否成功安装
    win10系统同时安装python2.7和python3.6
    Python 统计不同url svn代码变更数
    JavaWeb之 Servlet执行过程 与 生命周期
    JavaWeb之Servlet:请求 与 响应
    webservice(基础)
    通过反射,给对象之间赋值
    用反射获取类中的属性值
    tree树形
    破解weblogic(数据库)密码
  • 原文地址:https://www.cnblogs.com/immiao0319/p/15142491.html
Copyright © 2011-2022 走看看