简体   繁体   English

在此Spring Boot \\ MVC控制器中处理REST样式映射的正确方法是什么?

[英]What is the correct way to handle REST style mapping in this Spring Boot\MVC controller?

I am working on a Spring Boot application that implements REST web services. 我正在开发一个实现REST Web服务的Spring Boot应用程序。

So I have the following doubt about the URL that have to be used to handle insertion of an item in the database. 因此,对于用于处理数据库中项目插入的URL,我有以下疑问。

So basially I have this controller class: 所以基本上我有这个控制器类:

@RestController
@RequestMapping("/AccomodationMedia")
public class AccomodationMediaController {

    ..............................................................
    ..............................................................
    ..............................................................

    @RequestMapping(value = "/doUpload", method = RequestMethod.POST)
    public String handleFileUpload(HttpServletRequest request,
                                   @RequestParam MultipartFile[] fileUpload) throws Exception {

        System.out.println("handleFileUpload() START");
        if (fileUpload != null && fileUpload.length > 0) {
            for (MultipartFile currentFile : fileUpload){

                System.out.println("Saving file: " + currentFile.getOriginalFilename());

                accomodationMediaService.saveAccomodationMedia(currentFile);

            }
        }

        return "Success";
    }

}

So, this class contains the handleFileUpload() method that handle POST Http request toward the URI: /AccomodationMedia/doUpload to upload a file. 因此,此类包含handleFileUpload()方法,该方法处理对URI的POST Http请求: / AccomodationMedia / doUpload以上传文件。

This URI is not in rest style because it contains an action and in this case I have to handle it as resoure. 该URI不是静态样式,因为它包含一个动作,在这种情况下,我必须将其作为资源处理。

My doubt is: I think that to insert a new AccomodationMedia resource I only have to handhe POST requesto toward the /AccomodationMedia URI because I am inserting a new AccountMedia resource in the database. 我的疑问是:我认为要插入新的AccomodationMedia资源,只需要将POST requesto交给/ AccomodationMedia URI,因为我正在数据库中插入新的AccountMedia资源。 Is it correct? 这是正确的吗?

Yeah you are correct, each resource in a rest application should have at least one URI identifying it. 是的,您是正确的,其余应用程序中的每个资源都应至少具有一个标识它的URI。 And it's best when that URI makes sense and adequately describes the resource, just for example :- 最好是该URI有意义并充分描述资源,例如:-

To insert (create) a new customer in the system, we might use: POST http://www.example.com/customers 要在系统中插入(创建)新客户,我们可以使用:POST http://www.example.com/customers

To read a customer with Customer ID# 33245: GET http://www.example.com/customers/33245 The same URI would be used for PUT and DELETE, to update and delete, respectively. 要读取具有客户ID#33245的客户,请执行以下操作:GET http://www.example.com/customers/33245相同的URI将用于PUT和DELETE,分别用于更新和删除。

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

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