Spring开发RESTful服务(JSON)

最近学习了Spring,学习环境为Spring4.3.8 + Eclipse + JDK1.8。

使用Spring开发了一个简单的RESTful服务,客户端的请求和服务端的答复都是json格式。

步骤如下:

1、建立一网站。路径:File->New->Web->Dynamic Web Project。

2、导入Spring的jar包。红框内的jar包是必须要导入的,其他的jar包根据需要导入。

导入的jar包

3、配置Web.xml。需要在Web.xml中配置Spring的servlet。

<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

在Web项目中,Spring的配置文件(springmvc-servlet.xml)一般放在WEB-INF下面。其名称一般命名规则为servlet-name+"-servlet.xml",如果springmvc-servlet.xml。

4、创建Spring的配置文件

<?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-4.2.xsd
	 http://www.springframework.org/schema/mvc
	 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
	 http://www.springframework.org/schema/context
	 http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<!-- 扫描该包下类,生成bean -->
	<context:component-scan
		base-package="com.xhjz.controller" />

	<!-- 注解映射的支持。如:将json转成对象(输入),或将对象转成json(输出) -->
	<!-- 此方法对Spring4.0以后的不适用,改成方式2 -->
	<!-- 方式1-->
	<!-- <mvc:annotation-driven /> -->
	<!-- 方式2 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJackson2HttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="mappingJackson2HttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=utf-8</value>
			</list>
		</property>
	</bean>

</beans>

5、创建JSON模型类

package com.xhjz.controller;

public class MarketCfg {
	private String cfgId;
	private String cfgName;

	public String toString() {
		return "cfgId:" + cfgId + "|cfgName:" + cfgName;
	}

	public String getCfgId() {
		return cfgId;
	}

	public void setCfgId(String cfgId) {
		this.cfgId = cfgId;
	}

	public String getCfgName() {
		return cfgName;
	}

	public void setCfgName(String cfgName) {
		this.cfgName = cfgName;
	}
}
package com.xhjz.controller;

public class Result {
	private String RETCODE;
	private String RETMSG;

	public String getRETCODE() {
		return RETCODE;
	}

	public void setRETCODE(String rETCODE) {
		RETCODE = rETCODE;
	}

	public String getRETMSG() {
		return RETMSG;
	}

	public void setRETMSG(String rETMSG) {
		RETMSG = rETMSG;
	}

	public String toString() {
		return "{\"RETCODE\":\"" + this.RETCODE + "\",\"RETMSG\":\"" + this.RETMSG + "\"}";
	}

	public static Result create() {
		Result rst = new Result();
		rst.setRETCODE("1000");
		rst.setRETMSG("fail");
		return rst;
	}
}

6、创建Controller类

package com.xhjz.controller;

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;

@Controller
@RequestMapping(value = "/config")
public class Config {

	@ResponseBody
	@RequestMapping(value = "/add1", method = RequestMethod.POST)
	public Result add1(@RequestBody MarketCfg cfg) {

		Result rst = Result.create();

		if (cfg == null) {
			rst.setRETMSG("请求数据不合法或为空!");
			return rst;
		}

		if (cfg.getCfgId() == null || cfg.getCfgName() == null) {
			rst.setRETMSG("MarketCfg为空!");
			return rst;
		}

		String rspJson = cfg.toString();
		System.out.println(rspJson);

		return rst;
	}
}

7、测试

下载地址:https://download.csdn.net/download/tuolingss/11153967 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值