java 如何设计 Spring MVC REST 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4841498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:17:14  来源:igfitidea点击:

How to design a Spring MVC REST service?

javaspringrestspring-mvc

提问by outvir

I want the client and server application to talk to each other using REST services. I have been trying to design this using Spring MVC. I am looking for something like this:

我希望客户端和服务器应用程序使用 REST 服务相互通信。我一直在尝试使用 Spring MVC 来设计它。我正在寻找这样的东西:

  1. Client does a POST rest service call saveEmployee(employeeDTO, companyDTO)
  2. Server has a similar POST method in its controller saveEmployee(employeeDTO, companyDTO)
  1. 客户端执行 POST 休息服务调用 saveEmployee(employeeDTO, companyDTO)
  2. 服务器在其控制器中具有类似的 POST 方法 saveEmployee(employeeDTO, companyDTO)

Can this be done using Spring MVC?

这可以使用 Spring MVC 完成吗?

回答by esaj

Yes, this can be done. Here's a simple example (with Spring annotations) of a RESTful Controller:

是的,这是可以做到的。这是一个 RESTful 控制器的简单示例(带有 Spring 注释):

@Controller
@RequestMapping("/someresource")
public class SomeController
{
    @Autowired SomeService someService;

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public String getResource(Model model, @PathVariable Integer id)
    {
        //get resource via someService and return to view
    }

    @RequestMapping(method=RequestMethod.POST)
    public String saveResource(Model model, SomeResource someREsource)
    {
        //store resource via someService and return to view
    }

    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public String modifyResource(Model model, @PathVariable Integer id, SomeResource someResource)
    {
        //update resource with given identifier and given data via someService and return to view
    }

    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteResource(Model model, @PathVariable Integer id)
    {
        //delete resource with given identifier via someService and return to view
    }
}

Note that there are multiple ways of handling the incoming data from http-request (@RequestParam, @RequestBody, automatic mapping of post-parameters to a bean etc.). For longer and probably better explanations and tutorials, try googling for something like 'rest spring mvc' (without quotes).

请注意,有多种方法可以处理来自 http-request 的传入数据(@RequestParam、@RequestBody、后置参数到 bean 的自动映射等)。要获得更长且可能更好的解释和教程,请尝试在谷歌上搜索诸如“rest spring mvc”(不带引号)之类的内容。

Usually the clientside (browser) -stuff is done with JavaScript and AJAX, I'm a server-backend programmer and don't know lots about JavaScript, but there are lots of libraries available to help with it, for example see jQuery

通常客户端(浏览器)的东西是用 JavaScript 和 AJAX 完成的,我是一个服务器后端程序员,对 JavaScript 了解不多,但是有很多库可以帮助它,例如参见jQuery

See also: REST in Spring 3 MVC

另请参阅: Spring 3 MVC 中的 REST

回答by danny.lesnik

Yes, Rest is very easy to implement using spring MVC.

是的,Rest 使用 spring MVC 很容易实现。

@RequestMapping(value="/saveEmploee.do",method = RequestMethod.POST)
@ResponseBody
public void saveEmployee(@RequestBody Class myclass){
    //saving class.
    //your class should be sent as JSON and will be deserialized  by Hymanson
    //bean which should be present in your Spring xml.      
}