• 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就行了

  • 相关阅读:
    小白学Python(13)——pyecharts 绘制 柱状图/条形图 Bar
    小白学Python(12)——pyecharts ,生成词云图 WordCloud
    小白学Python(11)——pyecharts,绘制饼图 Pie
    小白学Python(10)——pyecharts 绘制仪表图 Gauge
    小白学Python(9)——pyecharts 绘制漏斗图 Funnel
    小白学Python(8)——pyecharts 入门
    小白学Python(7)——利用Requests下载网页图片、视频
    小白学Python(6)——python-pptx 添加图表
    python2/3 利用psycopg2 连接postgreSQL数据库。
    linux debian 9 / centos 7配置postgresSQL数据库
  • 原文地址:https://www.cnblogs.com/immiao0319/p/15142491.html
Copyright © 2020-2023  润新知