1.请求处理方法接受json参数
1.1创建项目
1.2完善项目
1.3导入依赖
<!-- 配置开发SpringMVC所以来的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- 配置ServletAPI依赖 -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
1.4配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcherServle</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServle</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
1.5配置SpringMVC配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置自动扫描包-->
<context:component-scan base-package="com.springmvc.controller"></context:component-scan>
</beans>
1.6在包com.springmvc.controller中编写自定义控制器类
2.接受服务器的请求参数
/**
* 1.接收param类型传参
* 实例地址:
* http://localhost:8080/demo/param/root/123456
*/
@RequestMapping(value="/param/{username}/{password}",method = RequestMethod.GET)
public void getUseInfoOne(@PathVariable("username") String name,
@PathVariable("password") String pass){
System.out.println("param请求");
System.out.println("姓名==>" + name);
System.out.println("密码==>" + pass);
}
/**
* 2.接受query类型传参实例地址:
* 实例地址:
* http://localhost:8080/demo/query?username=root&password123456
*/
@RequestMapping("/query")
public void getUseInfoTwo(@RequestParam("username") String name,
@RequestParam("password") String pass){
System.out.println("param请求");
System.out.println("姓名==>" + name);
System.out.println("密码==>" + pass);
}
/**
* 3.通过HttpServletRequest对象的getParameter获取传递的值。
* 实例地址:
* http://localhost:8080/demo/query?username=root&password123456
*/
public void getUseInfoTwo(HttpServletRequest req){
System.out.println("param类型请求");
System.out.println("姓名==>" + req.getParameter("username"));
System.out.println("密码==>" + req.getParameter("password"));
}
/**
* 4. 在请求处理方法中定义对应参数变量,参数变量的名称与页面元素的name属性值相同
* 实例地址:
* //http://127.0.0.1:8080/demo3/test/testvalue?username=lisi&password=111111
*/
@RequestMapping(value = "/testvalue")
public void testServlet(String username,String password){
System.out.println("原始的Servlet的请求方式");
System.out.println("处理用户请求--name=="+username+",pass=="+password);
}
/**
* 5. 【java对象的成员变量的名称一定要与页面元素的name属性值相同】
* 将需要提交的请求参数值封装到java对象中,通过Spring依赖注入setter方法完成
* http://127.0.0.1:8080/demo3/test/testvalue?username=lisi&password=111111
*/
@RequestMapping("studentType")
public void getUseInfoThree(StudentBean stu){
System.out.println(stu);
}
/**
* 6.接受json类型的数据,通过依赖注入将传入的json数据存放在StudenBean中
*/
@RequestMapping("jsonType")
public void getUseInfoJSON(@RequestBody StudentBean stu){
System.out.println(stu);
}
4.响应参数json类型
/** 响应json类型的参数
* 将bean类自动转换成json数据
*/
@RequestMapping("resjson")
@ResponseBody
public StudentBean getUserJsonData(){
return new StudentBean(2,"张三",20,"西安");
}
链接地址:http://localhost:8080/demo/resjson
响应结果
无奈源于不够强大