我在控制器中的post方法有问题
应用有2个模型类:
public class MovieGenre {
@Id
@Column(name = "genre_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long genreId;
@Column(name = "genre_name")
private String genreName;
@ManyToMany(mappedBy = "genres")
@JsonBackReference
private Set<Movie> movies = new HashSet<>();
// getters and setters
和
public class Movie {
@Id
@Column(name = "movie_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "movie_title")
private String title;
@Column(name = "production_year")
private String year;
@Column(name = "producer_name")
private String producer;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "movie_movie_genre",
joinColumns = @JoinColumn(name = "movie_id"),
inverseJoinColumns = @JoinColumn(name = "movie_genre_id"))
@JsonManagedReference
private Set<MovieGenre> genres = new HashSet<>();
// getters and setters
MovieRepository:
public interface MovieRepository extends JpaRepository<Movie, Long> {
}
电影服务实现了这样的服务
@Override
public Movie save(Movie object) {
return movieRepository.save(object);
}
现在控制器类
@RestController
@RequestMapping(value = "api/v1/movie", produces = MediaType.APPLICATION_JSON_VALUE)
public class MovieController {
private final MovieService movieService;
public MovieController(MovieService movieService) {
this.movieService = movieService;
}
@GetMapping
public ResponseEntity<Resources<Resource<Movie>>> getAll() {
Resources<Resource<Movie>> resources = new Resources<>(
movieService.findAll()
.map(this::createResource)
.collect(Collectors.toList())
);
resources.add(linkTo(methodOn(MovieController.class).getAll()).withSelfRel());
return ResponseEntity.ok().body(resources);
}
@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> addNewMovie(@RequestBody Movie newMovie) {
Movie addedMovie = movieService.save(newMovie);
return ResponseEntity
.created(URI.create(createResource(addedMovie).getLink("self").getHref()))
.build();
}
// some other code
Get方法可以正常工作,但是当我使用Postman或curl发送JSON时
{
"title": "Toy story 3",
"year": "2014",
"producer": "Pixar",
"genres": [
{
"genreId": 1,
"genreName": "action"
},
{
"genreId": 2,
"genreName": "animation"
},
{
"genreId": 3,
"genreName": "comedy"
}
]}
我正在使用InteliJ控制台:
2019-10-28 16:51:43.206 WARN 29729 --- [nio-8080-exec-3] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.progresspoint.movieapi.model.Movie]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (java.util.Set) not compatible with managed type (com.progresspoint.movieapi.model.Movie)
2019-10-28 16:51:43.212 WARN 29729 --- [nio-8080-exec-3] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.progresspoint.movieapi.model.Movie]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (java.util.Set) not compatible with managed type (com.progresspoint.movieapi.model.Movie)
2019-10-28 16:51:43.217 WARN 29729 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
邮递员中
{
"timestamp": "2019-10-28T15:51:43.225+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/json;charset=UTF-8' not supported",
"path": "/api/v1/movie/add"
}
我在stackoverflow上看到了很多关于生产/消费的问题-这对这里没有帮助 我在做什么错?
答案 0 :(得分:1)
将@RequestBody
与控制器中的方法参数之一一起使用时,无需指定消耗键。 @RequestBody建议它为application/json
。您可以尝试将其删除。
答案 1 :(得分:1)
将consumes = MediaType.APPLICATION_JSON_VALUE
的{{1}}删除,您无需提及。