项目结构;代码如下:BookControllerpackage com.mstf.controller;import javax.servlet.Http.httpservletResponse;import org.apache.comm
项目结构;
代码如下:
BookController
package com.mstf.controller;import javax.servlet.Http.httpservletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.codehaus.jackson.map.ObjectMapper;import com.mstf.domain.Book;import org.springframework.stereotype.Controller;import org.springframework.WEB.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/JSON")public class BookController { private static final Log logger = LogFactory.getLog(BookController.class); // @RequestMapping 根据 json 数据,转换成对应的 Object @RequestMapping(value="/testRequestBody") public void setJson(@RequestBody Book book,HttpServletResponse response) throws Exception { // ObjectMapper 类是 Jackson 库的主要类。他提供一些功能将 Java 对象转换成对应的 JSON ObjectMapper mapper = new ObjectMapper(); // 将 Book 对象转换成 json 输出 logger.info(mapper.writeValueAsString(book)); book.setAuthor("汪政"); response.setContentType("text/html;charset=UTF-8"); // 将 Book 对象转换成 json 写到客户端 response.getWriter().println(mapper.writeValueAsString(book)); }}
--结束END--
本文标题: Spring MVC登录注册以及转换json数据
本文链接: https://www.lsjlt.com/news/227077.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0