简体   繁体   English

Springboot如何在POST之后返回响应

[英]Springboot How to Return a response after a POST

I would like to create a new customer and return a customer number once the customer is created. 我想创建一个新客户并在创建客户后返回客户编号。 The customer number has to be an auto incremented unique number from 50000. 客户编号必须是从50000开始的自动递增的唯一编号。

Thus far i have managed to created a customer but i am not sure how i should go about generating the customer number, save it to the database and show it to the user as a success message when a POST is triggered. 到目前为止,我已经成功创建了一个客户,但是我不确定应该如何生成客户编号,将其保存到数据库中,并在触发POST时将其作为成功消息显示给用户。

Below json is the desired response; json下面是所需的响应;

{
    "customerNumber": "50002",
    "statusMessage": "Customer Created Successfully",
} 

And the following snippet from controller and service; 以及来自控制器和服务的以下代码段;

UserService.java UserService.java

public void createUser(User user) {
    if (user == null || user.getId() == null) {
        throw new ResourceNotFoundException("Empty", "Missing Data Exception");
    } else {
        userRepository.save(user);
    }
}

RegistrationController.java RegistrationController.java

@RequestMapping(method = RequestMethod.POST, value = "/users")
public void createUser(@RequestBody User user) {
    userService.createUser(user);
}

Annotate the class containing createUser with @RestController , or add @ResponseBody on createUser method directly, and change its return type to Response ; @RestController注释包含createUser的类,或直接在createUser方法上添加@ResponseBody ,并将其返回类型更改为Response

@RestController
class Controller {

    @RequestMapping(method = RequestMethod.POST, value = "/users")
    public Response createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
}

Assuming your createUser method in UserService will return a Response ; 假设您在UserServicecreateUser方法将返回Response

public Response createUser(User user) {
    if (user == null || user.getId() == null) {
        throw new ResourceNotFoundException("Empty", "Missing Data Exception");
    } else {
        User user = userRepository.save(user);
        return new Response(user.getId);
    }
}

Since fooRepository.save() always returns the successfully saved entity as long as it is the native save method from CrudRepository . 由于fooRepository.save()始终返回成功保存的实体,只要它是来自CrudRepository的本机save方法CrudRepository

The id field will be present inside the resulting User entity from save , to return the type of response you want, you'd need to create such a class, and transfer the aforementioned id ; id字段将出现在save的结果User实体中,以返回所需的响应类型,您需要创建这样的类,并传输上述id

class Response {

    private String customerNumber;
    private String statusMessage;

    public Response(String id) {
        this.customerNumber = id;
        this.statusMessage = "Customer Created Successfully";
    }

    // getters, setters, etc
}

For your controller to be able to return the id, the service layer should be able to provide that information. 为了使您的控制器能够返回ID,服务层应该能够提供该信息。

In other words, a service or the repository should be able to inform you the id of the last added user. 换句话说,服务或存储库应该能够通知您最后添加的用户的ID。 After getting that information, you can use it in the controller. 获取该信息后,您可以在控制器中使用它。

For example, some repository implementations set the id of the entity with the generated id after the saving (and commit of the transaction). 例如,某些存储库实现在保存(和提交事务)之后使用生成的ID设置实体的ID。 If that was your case, you could do: 如果是这种情况,您可以执行以下操作:

@RequestMapping(method = RequestMethod.POST, value = "/users")
@ResponseBody
public long createUser(@RequestBody User user){
     userService.createUser(user);
     return user.getId();
}

Notice the addition of the return clause, the returning type and the @ResponseBody annotation. 注意添加了return子句,返回类型和@ResponseBody批注。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM