搭建json格式的webservice服务器

搭建json格式的webservice服务器

本文为平时开发项目步骤记录,以备以后需要

一、所用技术 
语言:Java 
框架:spring mvc 3.1 
二、所用技术 
IDE:MyEclipse 
web容器: Apache tomcat 6.0 
三、所有详细步骤 
1. 前期准备工作,比如安装MyEclipse,安装tomcat这些网上有非常多的教程就不再赘述了。 
2. 打开MyEclipse,创建动态网站项目。 
创建项目 
输入项目名,选择在MyEclipse中配置好的tomcat6.0,如何在MyEclipse中配置tomcat网上也有很多教程。点击完成创建。 
这里写图片描述 
3. 在WEB-INF的lib文件夹下,导入所需jar包,http://download.csdn.net/detail/qq_36221405/9804218

4. 在web.xml插件中添加一下代码,用于加载spring mvc和字符解析:


<servlet>
        <servlet-name>ModelServer</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ModelServer</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在web.xml文件的同级目录创建SimpleService-servlet.xml文件,在这里配置spring mvc的相关配置。一下给出的模板是用于自动request body转化为Java class 和 上传文件的相关配置,其他只需再添加用户所需的依赖注入即可。


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-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 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <mvc:annotation-driven /> 
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>
                                text/html;charset=UTF-8
                            </value>
                        </list>
                    </property>
                </bean>
                <!-- 启动JSON格式的配置 -->
                <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation -->
                    <property name="supportedMediaTypes">
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <!--上传文件的配置 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 指定所上传文件的总大小不能超过2000KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
        <property name="maxUploadSize" value="20480000" />
        <!-- 设置在文件上传时允许写到内存中的最大值,以字节为单位计算,默认是10240 -->
        <!-- 但是经实验,上传文件大小若小于此参数,则不会生成临时文件,很尴尬,故改为256-->
        <property name="maxInMemorySize" value="256" />  

    </bean>
    </beans>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

至此整个框架已经搭建完毕,只需根据需求完成自己的代码即可。比如,想写一个接口,功能是别人发送GET请求,返回“hello world!”。

在src文件夹下创建Class(com.peter.Hello.java)


创建class


package com.peter;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/hello")   //此处value是rest api的前缀
public class Hello {
    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)//此处value是rest api的具体定位,method是接受的请求的类型
    public void sayHello(HttpServletResponse response){
        try {
            response.getWriter().write("{\"result\":\"hello world!\"}");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

然后将项目部署到已经搭建好的tomcat中,然后启动tomcat,然后使用rest请求发送这里写图片描述即可看到返回{result:hello world!}

至此整个简单项目已经搭建完毕,具体rest请求还有很多相关的东西,网上也有很多学习资料就不再赘述了。随手记录,如有问题,请博内留言以帮助我改正和完善,一起努力,一起学习,一起进步!


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WebService是一种基于网络的通信技术,用于不同系统之间的数据交换和通信。它支持多种数据传输格式,包括JSON(JavaScript Object Notation)。 当WebService接收到一个JSON格式的请求时,它会解析该请求,并根据其中的数据执行相应的操作。JSON是一种常用的数据交换格式,它使用简洁的文本来描述结构化数据。JSON由键值对组成,数据以键值对的形式表示,键和值之间使用冒号分隔,键值对之间使用逗号分隔。 对于WebService来说,接收JSON格式的请求需要通过一些特定的步骤来处理。首先,WebService需要解析接收到的JSON请求,将其转换为可操作的数据结构,通常是一个对象或类的实例。然后,WebService根据请求中的数据执行相关的业务逻辑。这可能涉及到对数据库的读写、调用其他服务的接口或执行其他计算等操作。最后,WebService将处理结果以JSON格式的响应返回给请求方。 要成功接收JSON格式的请求,WebService需要配置相应的请求处理器,以确保能够正确解析JSON数据并执行相应的操作。同时,在编写WebService的代码时,还需要确保能够正确处理和验证接收到的JSON数据,以防止潜在的安全问题。 总而言之,WebService可以接收JSON格式的请求,并根据其中的数据执行相应的操作。这种方式可以实现不同系统之间的数据交换和通信,极大地提高了系统之间的互操作性和数据共享能力。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值