【发布时间】:2022-01-23 20:19:04
【问题描述】:
我正在学习 SpringBoot,并且拥有能够理解传入 JSON 响应并将 JSON 响应返回给客户端 (POSTMAN) 的这段代码。我正在尝试体验 SpringBoot,并希望将其配置为返回 XML 响应。
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public ResponseEntity<Movie> addMovies(@RequestBody Movie movie) {
service.addToMovies(movie);
// Return a ResponseEntity instead.
return new ResponseEntity<Movie>(movie, HttpStatus.OK);
}
如果我使用值 application/xml 设置 Accept HTTP 标头,我会收到此错误:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class io.fireacademy.restapi.restapiaws.models.Movie] with preset Content-Type 'null']
不知道为什么会出现这个错误。另外,为什么它说,已解决? 我查看了现有的线程,发现我的 pom.xml 已经有条目:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
我的 SpringBoot 控制器类代码:
package io.fireacademy.restapi.restapiaws.controllers;
import io.fireacademy.restapi.restapiaws.models.Movie;
import io.fireacademy.restapi.restapiaws.services.MovieRecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/movies")
public class RestAPIController {
@Autowired
private MovieRecommendationService service;
// When to return a ResponseEntity
// Read https://stackoverflow.com/questions/49673660/return-responseentity-vs-returning-pojo
@GetMapping
public List<Movie> getMovies() {
return service.getMovies();
}
// Learning: The annotation @PathVariable is needed to inject the value in the URL onto the variable.
@GetMapping(path="/{movieId}")
public ResponseEntity<Movie> getMovieById(@PathVariable String movieId) {
return new ResponseEntity<Movie>(service.getMovie(Integer.parseInt(movieId)), HttpStatus.OK);
}
// TODO: Why is a List<Movie> failing in POST API and not in GET API
// TODO: What is the use of produces/consumes
// Learning: The client need to set Content-Type HTTP header set so that the server can understand how to process the body data.
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public ResponseEntity<Movie> addMovies(@RequestBody Movie movie) {
service.addToMovies(movie);
// Return a ResponseEntity instead.
return new ResponseEntity<Movie>(movie, HttpStatus.OK);
}
@PutMapping(path="/{movieId}")
public ResponseEntity<Movie> updateMovies(@PathVariable String movieId, @RequestBody Movie movie) {
// Return a ResponseEntity instead.
return new ResponseEntity<Movie>(service.updateAMovie(Integer.parseInt(movieId), movie), HttpStatus.OK);
}
@DeleteMapping(path="/{movieId}")
public ResponseEntity<Movie> deleteMovies(@PathVariable String movieId) {
// Return a ResponseEntity instead.
return new ResponseEntity<Movie>(service.removeAMovie(Integer.parseInt(movieId)), HttpStatus.OK);
}
// TODO: Integrate this onto a Javascript code
}
我也更新了消费者。还是一样的结果。
@PostMapping(consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
请给我任何线索。
【问题讨论】:
标签: spring-boot