提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
转帖|行业资讯|编辑:黄竹雯|2015-09-29 09:46:10.000|阅读 153 次
概述:Spring's annotation based MVC framework simplifies the process of creating RESTful web services. The key difference between a traditional Spring MVC controller and the RESTful web service controller is the way the HTTP response body is created.
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
Spring's annotation based MVC framework simplifies the process of creating RESTful web services. The key difference between a traditional Spring MVC controller and the RESTful web service controller is the way the HTTP response body is created. While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML. For a detailed description of creating RESTful web services using the Spring framework, click .
Figure 1: Spring MVC traditional workflow
The following steps describe a typical Spring MVC REST workflow:
In Figure 1, notice that in the traditional workflow the ModelAndView object is forwarded from the controller to the client. Spring lets you return data directly from the controller, without looking for a view, using the @ResponseBody annotation on a method. Beginning with Version 4.0, this process is simplified even further with the introduction of the @RestController annotation. Each approach is explained below.
When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the http response automatically. Each method in the Controller class must be annotated with @ResponseBody.
Figure 2: Spring 3.x MVC RESTful web services workflow
Spring has a list of HttpMessageConverters registered in the background. The responsibility of the HTTPMessageConverter is to convert the request body to a specific class and back to the response body again, depending on a predefined mime type. Every time an issued request hits @ResponseBody, Spring loops through all registered HTTPMessageConverters seeking the first that fits the given mime type and class, and then uses it for the actual conversion.
Let's walk through @ResponseBody with a simple example.
package com.example.spring.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Employee") public class Employee { String name; String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Employee() { } }
package com.example.spring.rest; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.example.spring.model.Employee; @RestController @RequestMapping("employees") public class EmployeeController { Employee employee = new Employee(); @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public Employee getEmployeeInJSON(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } @RequestMapping(value = "/{name}.xml", method = RequestMethod.GET, produces = "application/xml") public Employee getEmployeeInXML(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } }
Notice the @ResponseBody added to each of the @RequestMapping methods in the return value.
<context:component-scan>
and <mvc:annotation-driven />
tags to the Spring configuration file.<context:component-scan>
activates the annotations and scans the packages to find and register beans within the application context. <mvc:annotation-driven/>
adds support for reading and writing JSON/XML if the Jackson/JAXB libraries are on the classpath. //localhost:8080/SpringRestControllerExample/rest/employees/Bob
and the following output displays://localhost:8080/SpringRestControllerExample/rest/employees/Bob.xml
and the following output displays:
Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default. Click to learn more.
Figure 5: Spring 4.x MVC RESTful Web Services Workflow
To use @RestController in our example, all we need to do is modify the @Controller to @RestController and remove the @ResponseBody from each method. The resultant class should look like the following:
package com.example.spring.rest; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.example.spring.model.Employee; @RestController @RequestMapping("employees") public class EmployeeController { Employee employee = new Employee(); @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public Employee getEmployeeInJSON(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } @RequestMapping(value = "/{name}.xml", method = RequestMethod.GET, produces = "application/xml") public Employee getEmployeeInXML(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } }
Note that we no longer need to add the @ResponseBody to the request mapping methods. After making the changes, running the application on the server again results in same output as before.
As you can see, using @RestController is quite simple and is the preferred method for creating MVC RESTful web services starting from Spring v4.0. I would like to extend a big thank you to my co-author, Swapna Sagi, for all of her help in bringing you this information!
Source
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@capbkgr.cn
通过提供强大的3D CAD数据访问工具并适用于桌面、移动和Web的高级环境3D可视化发动机,HOOPS在提升造船设计和制造流程的效率方面发挥了重要作用。
HOOPS Luminate在汽车行业中的应用具有广泛的潜力和深远的影响。它通过提供高效的3D可视化、虚拟装配与拆解、性能分析、客户定制等功能,帮助汽车制造商在设计、生产和销售过程中提升效率、降低成本并提高产品质量。
在不断发展的软件开发世界中,使工具和框架与最新的平台版本保持同步至关重要,欢迎查阅~
全球航运业对国际贸易至关重要,全球 90% 以上的商品通过海运运输。准确监控和控制这些集装箱的移动对于维持高效的供应链至关重要。手动输入集装箱号码是这一程序的关键部分,它带来了相当大的挑战,例如人为错误和效率低下。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@capbkgr.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢