没有找到合适的产品?
联系客服协助选型:023-68661681
提供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@dpuzeg.cn
HOOPS Visualize 正是为满足此类需求而生的一款高性能3D图形渲染SDK,凭借其稳定的图形引擎架构、丰富的交互能力和高度定制性,已成为多个行业中间件和工业软件厂商的首选。
本文将为大家介绍QtitanChart在制造行业场景中的落地应用探索,欢迎下载最新版体验!
Parasoft Jtest以其强大的“度量+去重”功能组合,为开发者提供了一种高效、可靠的解决方案。通过深入洞察代码的本质,优化代码结构,Jtest不仅有助于提升开发效率,还能显著提高代码质量,减少软件缺陷的产生。
Parasoft C/C++test 通过静态分析、安全漏洞检测和行业标准合规检查,帮助开发者在编码阶段即时锁定缺陷,大幅减少后期返工,确保了代码安全性与行业合规性,真正实现开发效率与产品质量的双重提升。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@dpuzeg.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢