Java POST方法中存在不支持的媒体类型错误
我对控制器中的post方法有问题 应用程序获得了2个模型类:Java POST方法中存在不支持的媒体类型错误,java,json,spring,Java,Json,Spring,我对控制器中的post方法有问题 应用程序获得了2个模型类: public class MovieGenre { @Id @Column(name = "genre_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long genreId; @Column(name = "genre_name") private String genreName; @ManyToMa
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
现在是控制器类
@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
我在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上看到了许多关于产品/消费的问题——这在这里没有帮助
我做错了什么 当您将
@RequestBody
与控制器中的一个方法参数一起使用时,无需指定consumes键@RequestBody建议它是application/json
。您可以尝试删除它。删除consumes=MediaType.APPLICATION\u JSON\u VALUE
对于@PostMapping
,您不必提及这一点。@Deadpool我在电影和电影类型之间有双向关系,我想创建联接表-这就是我使用这些注释的原因。我还能怎么做?谢谢。我不知道。我把它拿走了。这对你没有帮助:)你能分享你调用服务url的方式吗。是否提供了所有标题、参数等?
@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
{
"title": "Toy story 3",
"year": "2014",
"producer": "Pixar",
"genres": [
{
"genreId": 1,
"genreName": "action"
},
{
"genreId": 2,
"genreName": "animation"
},
{
"genreId": 3,
"genreName": "comedy"
}
]}
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"
}