Maven项目:Spring MVC + Ajax + Json + RequestBody:POST后台服务器接收前端JSON数据并注解到POJO内

xiaoxiao2021-02-28  126

最近一直看Java服务器端开发,用的架构在Maven项目内的Web后台服务器,用了两天时间明白了怎么整合这些框架,很累找了很多资料,尤其在整个过程中Eclipse还时不时崩溃,正常的jsp文件都打不开,需要重新启动编译器,另外还有不断的调试。由于本人对于前端不太了解,更多时候调节一堆错误404,415这些错误真的很烦人,有时候重新启动编译器可以,但是有时候就需要查看POST的数据,而网上的大部分东西要么写的不全,要不就是随意写写,测试结果用例都没有,另外错误都不一样,对于我这样新手很不 友好,难以理解,好了吐槽完毕,整体创建一遍希望有用吧。 1、创建Maven项目 在这里不陈述如何创建项目了,选择过程中Packaging选择war就行,如果生成项目没有web.xml,可能项目会报错,可以如下所示点击创建web.xml,为了省事用的上一篇文章的截图: 项目的整体框架如下所示:

2、添加JAR开发包 因为最近两天比较忙,所以就直接把网上下载的Spring框架包直接粘贴到了lib里面没有挑选,然后红框里面的需要读者自行下载,而Spring框架的JAR包可以看我上一篇文章 创建Spring第一个程序HelloWorld,里面有地址可以下载最新的版本。 3、配置web.xml文件

<servlet> <servlet-name>SpringDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

4、配置springmvc.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <mvc:annotation-driven/> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.gyc.springmvc"></context:component-scan> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name = "prefix" value="/WEB-INF/views/"></property> <property name = "suffix" value = ".jsp"></property> </bean> </beans>

5、pom.xml引入依赖关系

<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.2</version> </dependency> </dependencies>

6、测试前端testjson.jsp文件 这里主要用到了Ajax的JQuery,进行JSon数据传输,使用POST传输方式

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript"> $(function(){ $("button").click(function(){ $.ajax({ type:"POST", url:"testJson", contentType:"application/json;charset=utf-8", dataType:"json", data:JSON.stringify({ "Name": "test", "Password": "123456", "Address": "SD" }), success:function(){ console.log("success"); } }); }); }) </script> </head> <body> <button>Click</button> </body> </html>

7、创建User类文件

在这里一定要郑重的声明一下:

太坑爹了,一天的时间就是测试如何用RequestBody注解到类内,但是后台收到数据为null,而且找了半天网上信息都是前篇一律,可是皇天不负有心人啊,找到了解决办法,就是在数据POST到后台以后注解可能Json数据难以一一对应赋值,那怎么办呢在类的每一个上边添加@JsonProperty(value=”xxxx”)一定要写对啊,要不找不到就注解不进去,不写出来心情不爽啊,这个办法地址为spring中使用@RequestBody 接收到的对象值为空,太感谢了,没有白让我一天忙活啊。

import com.fasterxml.jackson.annotation.JsonProperty; public class User { @JsonProperty(value="Name") private String Name; @JsonProperty(value="Password") private String Password; @JsonProperty(value="Address") private String Address; public String getName() { return Name; } public void setName(String name) { Name = name; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } @Override public String toString() { // TODO Auto-generated method stub return "User:" + Name + "\n" + "Password:" + Password + "\n" + "Address:" + Address; } }

8、编写Controller

import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.gyc.models.User; @Controller public class TestJson { @RequestMapping(value="/testJson", method=RequestMethod.POST, produces="application/json;charset=utf-8") @ResponseBody public void testJson(@RequestBody User user) { System.out.println("success"); System.out.println(user); } }

9、测试结果 两天的时间搞明白了如何注解POJO,很开心,也十分苦恼,编程不易且行且珍惜啊。

转载请注明原文地址: https://www.6miu.com/read-34617.html

最新回复(0)