简体   繁体   English

Spring API REST Controller 在 POST 方法后返回 HTTP 406 而不是 HTTP 201

[英]Spring API REST Controller returning HTTP 406 instead of HTTP 201 after POST method

I am building and testing a simple Spring Boot API REST tutorial.我正在构建和测试一个简单的 Spring Boot API REST 教程。 I have faced with an issue that I am trying to understand.我遇到了一个我试图理解的问题。 I am getting an HTTP 406 (NOT ACCEPTABLE) when calling POST method to create and persist a new entity.调用 POST 方法创建和保留新实体时,我收到 HTTP 406(不可接受)。

Problem is the entity is persisted but the response to the client is not what should be expected (HTTP 201 CREATED with URI in this case).问题是实体是持久化的,但对客户端的响应不是预期的(在这种情况下,HTTP 201 CREATED with URI)。

Tutorial and TutorialDto classes have the exact same attributes. Tutorial 和 TutorialDto 类具有完全相同的属性。 Here is the definition:这是定义:

public class TutorialDto {
    private long id;
    private String title;
    private String description;
    private boolean published;

...
}

Here is my POST method in @RestController class:这是我在@RestController class 中的 POST 方法:

@PostMapping("/tutorial")
public ResponseEntity.BodyBuilder createTutorial(@RequestBody final TutorialDto tutorialDto) {
    final TutorialDto createdTutorial = tutorialService.add(tutorialDto);
    return ResponseEntity.created(URI.create(String.format("tutorial/%d", createdTutorial.getId())));
}

And here is the @Service method to create the entity:这是创建实体的@Service 方法:

@Transactional
public TutorialDto add(final TutorialDto tutorialDto) {
    final Tutorial createdTutorial = tutorialRepository.save(modelmapper.map(tutorialDto, Tutorial.class));
    return Optional.of(modelmapper.map(createdTutorial, TutorialDto.class))
            .orElseThrow(() -> new TutorialCreationException(
                    String.format("Tutorial: %s could not be created", tutorialDto.getTitle()))
            );
}

This is the request body:这是请求正文:

{
    "title": "tutorial",
    "description": "This is the first created tutorial"
}

And this is the response body:这是响应主体:

{
    "timestamp": "2022-04-16T00:40:36.626+00:00",
    "status": 406,
    "error": "Not Acceptable",
    "path": "/api/v1/tutorial"
}

I am getting the HTTP 406 response at the end of the controller method, after returning the "ResponseEntity.created".返回“ResponseEntity.created”后,我在 controller 方法末尾收到 HTTP 406 响应。

Thanks in advance.提前致谢。

Looks like you are using wrong usage of ResponseEntity.BodyBuilder.看起来您错误地使用了 ResponseEntity.BodyBuilder。 Here is an example 这是一个例子

Hence, your controller code should look something like this:因此,您的 controller 代码应如下所示:

@PostMapping("/tutorial")
public ResponseEntity createTutorial(@RequestBody final TutorialDto tutorialDto) {
    final TutorialDto createdTutorial = tutorialService.add(tutorialDto);
    return ResponseEntity.created(URI.create(String.format("tutorial/%d", createdTutorial.getId()))).body(createdTutorial);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring RestController:Rest Controller 中抛出异常时的 HTTP 406 - Spring RestController: HTTP 406 when an exception is thrown in Rest Controller HTTP 状态 406 在 rest api Z2A2D595E6ED9A0B234F027F2B6B6B 启动时 - HTTP Status 406 in rest api spring boot when getByEmail Spring MVC返回HTTP 406响应 - Spring MVC Returning HTTP 406 Response 获取 HTTP 状态 406 – 尝试在 Spring 中返回 object 时出现不可接受的错误 REST controller - Getting HTTP Status 406 – Not Acceptable error when trying to return an object in Spring REST controller Spring REST API中自定义接受/标题和返回类型的406 HTTP状态 - 406 HTTP Status for custom accept/header and return type in Spring REST API $ http不从Spring MVC REST控制器返回值 - $http not returning values from Spring MVC REST controller Spring 引导 REST Controller 集成测试返回 406 而不是 500 - Spring Boot REST Controller Integration Test returns 406 instead of 500 spring 中的 POST 方法测试。 代码 400 而不是 201 - POST method test in spring. Code 400 instead of 201 HTTP状态406 –在Spring MVC Rest Service中不可接受 - HTTP Status 406 – Not Acceptable in spring MVC Rest Service 在Spring上调用rest Web服务时出错:HTTP状态406 - Error to call a rest web service on spring : HTTP Status 406
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM