gpt4 book ai didi

java - POST 方法中出现不支持的媒体类型错误

转载 作者:行者123 更新时间:2023-11-30 01:44:38 27 4
gpt4 key购买 nike

我在 Controller 中使用 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

电影存储库:

public interface MovieRepository extends JpaRepository<Movie, Long> {
}

电影服务实现这样的服务

@Override
public Movie save(Movie object) {
return movieRepository.save(object);
}

现在 Controller 类

@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]

在 postman

{
"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 上看到了很多关于生产/消费的问题 - 这在这里没有帮助我做错了什么?

最佳答案

当您将 @RequestBody 与 Controller 中的方法参数之一一起使用时,无需指定消耗键。 @RequestBody 建议它是 application/json。您可以尝试删除它。

关于java - POST 方法中出现不支持的媒体类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com