Java教程

多米诺披萨oa

本文主要是介绍多米诺披萨oa,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

英文题目

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

 

这篇关于多米诺披萨oa的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!