REST服务使用@RestController实例,输出xml/json
REST服务使用@RestController实例,输出xml/json
需要用到的服务注解
org.springframework.web.bind.annotation.RestController
用到的包:java和json互转
jackson-mapper-asl #//java对象和json相互转化
jackson-databind #spring4.1+以上都必须包含(java对象和json相互转化)
访问地址:
http://localhost:8080/gugua9/hello/aaa.xml
http://localhost:8080/gugua9/hello/aaa.json

pom.xml
<!-- 版本号 -->
<properties>
<springVersion>4.3.5.RELEASE</springVersion>
</properties> <dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- spring-test支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!-- spring模块库 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency> <!-- Servlet dependencies -->
<!-- servlet(HttpServletRequest,HttpServletResponse) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- servlet(HttpServletRequest,HttpServletResponse) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<!-- java对象和json相互转化 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency> </dependencies> <build>
<pluginManagement>
<plugins>
<plugin>
<!-- 需要声明maven-plugin版本,用来管理包,否则会报错 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>gugua9</warName>
<!-- 屏蔽web.xml -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>gugua9</finalName>
</build>
message.java
加入xml注解就支持xml,不加入只支持json
package springmvc.domain; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; //适应XML输出
@XmlRootElement(name="message")
public class Message { String name;
String text; public Message(){ } public Message(String name, String text) {
super();
this.name = name;
this.text = text;
} @XmlElement
public String getName() {
return name;
} @XmlElement
public String getText() {
return text;
} }
mvc/config 配置开启
package springmvc.configuration; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; //开启mvc,配置类
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="springmvc")
public class HelloWorldConfiguration { }
初始化
package springmvc.configuration; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; //初始化
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[] { HelloWorldConfiguration.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
} @Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[]{"/"};
} }
控制器:
package springmvc.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import springmvc.domain.Message; @RestController
public class HelloWorldRestController { @RequestMapping(value="/hello/{player}")
public Message message(@PathVariable String player )
{
Message msg = new Message(player, "hello," + player); return msg; } }
REST服务使用@RestController实例,输出xml/json的更多相关文章
- Spring4 MVC REST服务使用@RestController实例
在这篇文章中,我们将通过开发使用 Spring4 @RestController 注解来开发基于Spring MVC4的REST风格的JSON服务.我们将扩展这个例子通过简单的注释与JAXB标注域类支 ...
- 前端技术之:如何在控制台将JS class实例输出为JSON格式
有一个类: class Point { constructor(x, y) { this.x = x; this.y = y; } } 如果我们在控制台中输出其实例: console.log(new ...
- Xml,Json,Hessian,Protocol Buffers序列化对比
简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...
- yii2 输出xml格式数据
作者:白狼 出处:http://www.manks.top/yii2_xml_response.html.html本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文 ...
- xml json protobuf
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...
- Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题
1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...
- Query通过Ajax向PHP服务端发送请求并返回JSON数据
Query通过Ajax向PHP服务端发送请求并返回JSON数据 服务端PHP读取MYSQL数据,并转换成JSON数据,传递给前端Javascript,并操作JSON数据.本文将通过实例演示了jQuer ...
- 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象 分类: JSON 前端 2014-11-10 10:20 97人阅读 评论(1) 收藏
一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...
- PHP实例——输出安全的HTML代码
原文:PHP实例--输出安全的HTML代码 //输出安全的htmlfunction h($text, $tags = null){ $text = trim($text); //完全过滤注释 $tex ...
随机推荐
- 阿里云服务器被挖矿程序minerd入侵的终极解决办法[转载]
突然发现阿里云服务器CPU很高,几乎达到100%,执行 top c 一看,吓一跳,结果如下: root 386m S : /tmp/AnXqV -B -a cryptonight -o stratum ...
- 用Squid实现反向代理
Last-Modified: 告诉反向代理页面什么时间被修改 Expires: 告诉反向代理页面什么时间应该从缓冲区中删除 Cache-Control: 告诉反向代理页面是否应该被缓冲 Pragma: ...
- Spring.Net的使用
1.Spring.Net的简单介绍 spring.net 框架是微软效仿java中的spring框架而推出的一种在.net中使用的框架,它使用配置的方式实现逻辑的解耦,它的主要功能集成在Spring. ...
- SaltStack实现Haproxy Nginx+php MySQL主从
构建图如下 参考文档:https://github.com/unixhot/saltbook-code/tree/master
- 解决 Ubuntu 下 Sublime Text 无法输入中文的问题
解决 Ubuntu 下 Sublime Text 无法输入中文的问题 1. 安装依赖库 sudo apt-get install build-essential sudo apt-get instal ...
- Python知识总汇
一.python基础 python基础 python编码问题 逻辑运算 二.python数据类型 二.python数据类型 三.IO(文件处理) 三.IO(文件处理) 四.函数 函数基础 名称空间与作 ...
- MegaCli 监控raid状态
MegaCli 监控raid状态 http://blog.chinaunix.net/uid-25135004-id-3139293.html 简介 MegaCli是一款管理维护硬件RAID软件,可以 ...
- Java API操作HA方式下的Hadoop
通过java api连接Hadoop集群时,如果集群支持HA方式,那么可以通过如下方式设置来自动切换到活动的master节点上.其中,ClusterName 是可以任意指定的,跟集群配置无关,dfs. ...
- ABP常见问题
System.Data.SqlClient.SqlException (0x80131904): 'OFFSET' 附近有语法错误 解决方案:最新的ABP默认支持的是sql2012以上的版本,对于之前 ...
- 表单验证—js循环所有表单验证
[封装为表单验证的专用js,所有表单页面都可以调用] 1.表单 <form id="regForm" method="post" action=" ...