- Spring 5.0 By Example
- Claudio Eduardo de Oliveira
- 201字
- 2021-06-24 19:17:35
NewsResource
The NewsResource class is essential, this endpoint enables users to review news previously registered, and it also provides an endpoint to return the updated news. This is an important feature because we are interested only in the relevant news. Irrelevant news cannot be shown on the portal. The resource class should look like this:
package springfive.cms.domain.resources;
import java.util.Arrays;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import springfive.cms.domain.models.News;
import springfive.cms.domain.models.Review;
import springfive.cms.domain.vo.NewsRequest;
@RestController
@RequestMapping("/api/news")
public class NewsResource {
@GetMapping(value = "/{id}")
public ResponseEntity<News> findOne(@PathVariable("id") String id){
return ResponseEntity.ok(new News());
}
@GetMapping
public ResponseEntity<List<News>> findAll(){
return ResponseEntity.ok(Arrays.asList(new News(),new News()));
}
@PostMapping
public ResponseEntity<News> newNews(NewsRequest news){
return new ResponseEntity<>(new News(), HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void removeNews(@PathVariable("id") String id){
}
@PutMapping("/{id}")
public ResponseEntity<News> updateNews(@PathVariable("id") String id,NewsRequest news){
return new ResponseEntity<>(new News(), HttpStatus.OK);
}
@GetMapping(value = "/{id}/review/{userId}")
public ResponseEntity<Review> review(@PathVariable("id") String id,@PathVariable("userId") String userId){
return ResponseEntity.ok(new Review());
}
@GetMapping(value = "/revised")
public ResponseEntity<List<News>> revisedNews(){
return ResponseEntity.ok(Arrays.asList(new News(),new News()));
}
}
The NewsRequest class can be found at GitHub.
Pay attention to the HTTP verbs and the HTTP status code, as we need to follow the correct semantics.
推薦閱讀
- 大學(xué)計算機(jī)基礎(chǔ)(第二版)
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- Getting started with Google Guava
- 精通API架構(gòu):設(shè)計、運維與演進(jìn)
- Mastering Swift 2
- Amazon S3 Cookbook
- Rust Essentials(Second Edition)
- 劍指大數(shù)據(jù):企業(yè)級數(shù)據(jù)倉庫項目實戰(zhàn)(在線教育版)
- Mastering Git
- JavaScript程序設(shè)計(第2版)
- Android Studio開發(fā)實戰(zhàn):從零基礎(chǔ)到App上線 (移動開發(fā)叢書)
- VMware vSphere 5.5 Cookbook
- LabVIEW入門與實戰(zhàn)開發(fā)100例(第4版)
- Java EE 7 Development with WildFly
- C語言編程魔法書:基于C11標(biāo)準(zhǔn)