ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

多米诺披萨oa

2021-08-14 03:00:30  阅读:256  来源: 互联网

标签:articles Article 披萨 springframework oa 多米诺 import org id


英文题目

JSON Articles Controller
In this challenge we're going to start to build a JSON controller as a way to retreieve articles from the API.

Endpoints
We'll need to handle the following two endpoints.

Description

GET /articles - An endpoint for retrieving all Article stored.
GET /articles/{id} - An endpoint for retrieving a specific Article by it's id.

Return JSON

The endpoints should return JSON representations of Article and List<Article> for GET /articles and GET /articles/{id} respectively.

Without Data

If the GET /articles endpoint has no articles, it should return an empty list.
If the GET /articles{id} endpoint finds no article it should return nothing and set a 404 response code.

Article Service
The controller will have access to the ArticleService through which it can obtain articles.
Two methods that are of particular utility to this challenge in the ArticleService are:

@Service
class ArticleService {
// will retrieve all articles stored
public List<Article> getAll();
// will use the id to find an article with the same id
// if none is found, it will return null
public Article findById(int id);
}
Article Model
The Article model has three properties with getters and setters for each:

Title (String) - getTitle/setTitle
Body (String) - getBody/setBody
Id (int) - getId/setId

 

中文题目

JSON 文章控制器
在这个挑战中,我们将开始构建一个 JSON 控制器,作为从 API 检索文章的一种方式。

端点
我们需要处理以下两个端点。

描述

GET /articles - 用于检索存储的所有文章的端点。
GET /articles/{id} - 通过 id 检索特定文章的端点。

返回 JSON

端点应分别为 GET /articles 和 GET /articles/{id} 返回 Article 和 List<Article> 的 JSON 表示。

没有数据

如果 GET /articles 端点没有文章,它应该返回一个空列表。
如果 GET /articles{id} 端点没有找到文章,它应该不返回任何内容并设置 404 响应代码。

文章服务
控制器可以访问 ArticleService,通过它可以获得文章。
对 ArticleService 中的这一挑战特别有用的两种方法是:

@服务
类文章服务{
// 将检索存储的所有文章
公共列表<文章> getAll();
// 将使用 id 查找具有相同 id 的文章
// 如果没有找到,则返回 null
公共文章 findById(int id);
}
文章模型
Article 模型具有三个属性,每个属性都有 getter 和 setter:

标题(字符串)- getTitle/setTitle
正文(字符串)- getBody/setBody
Id (int) - getId/setId

 

solution

package articles;

import org.springframework.web.bind.annotation.*;

@RestController
public class ArticlesController {
    private final ArticleService service;
    public ArticlesController(ArticleService service) {
        this.service = service;
    }   
}

test

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);
        }
    }  
}

 

标签:articles,Article,披萨,springframework,oa,多米诺,import,org,id
来源: https://www.cnblogs.com/immiao0319/p/15139729.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有