栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java jQuery,Spring MVC @RequestBody和JSON-使之协同工作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java jQuery,Spring MVC @RequestBody和JSON-使之协同工作

我很确定你只需要注册

MappingJacksonHttpMessageConverter

(最简单的方法是通过

<mvc:annotation-driven />XML
@EnableWebMvcJava
来实现)

这是一个工作示例:

Maven POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">    <modelVersion>4.0.0</modelVersion><groupId>test</groupId><artifactId>json</artifactId><packaging>war</packaging>    <version>0.0.1-SNAPSHOT</version><name>json test</name>    <dependencies>        <dependency><!-- spring mvc --> <groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.0.5.RELEASE</version>        </dependency>        <dependency><!-- jackson --> <groupId>org.prehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.4.2</version>        </dependency>    </dependencies>    <build><plugins> <!-- javac --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version><configuration><source>1.6</source><target>1.6</target></configuration></plugin> <!-- jetty --><plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId> <version>7.4.0.v20110414</version></plugin>    </plugins></build></project>

在文件夹src / main / webapp / WEB-INF中

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"    version="2.4">    <servlet><servlet-name>json</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>json</servlet-name>        <url-pattern>/*</url-pattern>    </servlet-mapping></web-app>

json-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd">    <import resource="classpath:mvc-context.xml" /></beans>

在文件夹src / main / resources中:

mvc-context.xml<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <mvc:annotation-driven />    <context:component-scan base-package="test.json" /></beans>

在文件夹src / main / java / test / json中

TestController.java

@Controller@RequestMapping("/test")public class TestController {    @RequestMapping(method = RequestMethod.POST, value = "math")    @ResponseBody    public Result math(@RequestBody final Request request) {        final Result result = new Result();        result.setAddition(request.getLeft() + request.getRight());        result.setSubtraction(request.getLeft() - request.getRight());        result.setMultiplication(request.getLeft() * request.getRight());        return result;    }}

Request.java

public class Request implements Serializable {    private static final long serialVersionUID = 1513207428686438208L;    private int left;    private int right;    public int getLeft() {return left;}    public void setLeft(int left) {this.left = left;}    public int getRight() {return right;}    public void setRight(int right) {this.right = right;}}

Result.java

public class Result implements Serializable {    private static final long serialVersionUID = -5054749880960511861L;    private int addition;    private int subtraction;    private int multiplication;    public int getAddition() { return addition; }    public void setAddition(int addition) { this.addition = addition; }    public int getSubtraction() { return subtraction; }    public void setSubtraction(int subtraction) { this.subtraction = subtraction; }    public int getMultiplication() { return multiplication; }    public void setMultiplication(int multiplication) { this.multiplication = multiplication; }}

你可以通过mvn jetty:run在命令行上执行,然后发送POST请求来测试此设置:

URL:        http://localhost:8080/test/mathmime type:  application/jsonpost body:  { "left": 13 , "right" : 7 }

我使用了海报Firefox插件来执行此操作。

响应如下所示:

{"addition":20,"subtraction":6,"multiplication":91}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号