`
xglv2013
  • 浏览: 38059 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring MVC实现的RESTful webservice服务器并用Python调用API

阅读更多
    下面使用spring MVC+jpa实现一个RESTful webservice服务器,然后用python调用API实现资源的转移,数据库使用mysql,本文仅仅是为了起到一个演示的作用,所以无论是源代码还是配置文件,都只写了有关怎样配置restful服务的部分,关于其他spring的配置问题请参阅相关文档。有什么不当之处,还请不吝赐教!
    第一步,在mysql中建立一个名为spitter的数据库,使用如下的建表语句建立一个用户表spitter,一个发文表spittle:
    CREATE TABLE `spitter` (
    `id` int(11) default NULL,
    `username` char(20) default NULL,
    `password` char(20) default NULL,
    `fullName` char(40) default NULL,
    `email` char(80) default NULL,
    `update_by_email` tinyint(1) default NULL);
   
    CREATE TABLE `spittle` (
    `id` int(11) default NULL,
    `spittleText` char(200) default NULL,
    `postedTime` datetime default NULL,
    `spitter_id` int(11) default NULL);

    在用户表插入一条记录:
    insert into spitter values('48180', 'john', '19850115', 'john Donkey', 'xglvddz@gmail.com', 1);

    在发文表插入两条记录,表示这个用户发布了两条微博:
    insert into spittle values ('1', 'i live in beijing', now(), '48180');
    insert into spittle values ('2', 'today is sunny', now(), '48180');
   
    第二步开始编写java代码,怎样建工程,建包在此都省略,
(1)建立spitter控制器:
package com.habuma.spitter.mvc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.habuma.spitter.domain.Spittle;
import com.habuma.spitter.service.SpitterService;

@Controller
@RequestMapping("/spitters")
public class SpitterController {
  private final SpitterService spitterService;
  
  @Autowired
  public SpitterController(SpitterService spitterService) 
  {
    this.spitterService = spitterService;
  }
  //以下方法的RequestMapping注解中的value属性值表示资源的uri为
  //servlet上下文+/spitters/{username}/spittles,
  //method的值表示客户端请求为GET,
  //headers的值表示只有客户端HTTP请求头的Accept值为application/json时才会进入这个方法
  @RequestMapping(value = "/{username}/spittles", 
                  method = RequestMethod.GET, 
                  headers = "Accept=application/json")
  public @ResponseBody  
  List<Spittle> getSpittlesForSpitter(@PathVariable String username)
  {
    return spitterService.getSpittlesForSpitter(username);
  }
}

(2)建立服务层:
package com.habuma.spitter.service;

import java.util.List;

import com.habuma.spitter.domain.Spitter;
import com.habuma.spitter.domain.Spittle;

public interface SpitterService {

  List<Spittle> getSpittlesForSpitter(Spitter spitter);
  List<Spittle> getSpittlesForSpitter(String username);
  Spitter getSpitter(String username);
  
}

package com.habuma.spitter.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.habuma.spitter.domain.Spitter;
import com.habuma.spitter.domain.Spittle;
import com.habuma.spitter.persistence.SpitterDao;

@Service("spitterService")
@Transactional(propagation=Propagation.REQUIRED)
public class SpitterServiceImpl implements SpitterService {

  public List<Spittle> getSpittlesForSpitter(Spitter spitter) {
    return spitterDao.getSpittlesForSpitter(spitter);
  }

  public List<Spittle> getSpittlesForSpitter(String username) {
    Spitter spitter = spitterDao.getSpitterByUsername(username);
    return getSpittlesForSpitter(spitter);
  }

  public Spitter getSpitter(String username) {
    return spitterDao.getSpitterByUsername(username);
  }
  
  @Autowired
  SpitterDao spitterDao;

}

(3)建立Dao:
package com.habuma.spitter.persistence;

import java.util.List;

import com.habuma.spitter.domain.Spitter;
import com.habuma.spitter.domain.Spittle;

public interface SpitterDao {
  
  List<Spittle> getSpittlesForSpitter(Spitter spitter);

  Spitter getSpitterByUsername(String username);
}

//<start id="java_contextualJpaDao"/> 
package com.habuma.spitter.persistence;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.habuma.spitter.domain.Spitter;
import com.habuma.spitter.domain.Spittle;

@Repository("spitterDao")
@Transactional
public class JpaSpitterDao implements SpitterDao {

  private static final String SPITTER_FOR_USERNAME = 
      "SELECT s FROM Spitter s WHERE s.username = :username";
  private static final String SPITTLES_BY_USERNAME = 
      "SELECT s FROM Spittle s WHERE s.spitter.username = :username";

  @PersistenceContext
  private EntityManager em;

  @SuppressWarnings("unchecked")
  public List<Spittle> getSpittlesForSpitter(
          Spitter spitter) {
    return (List<Spittle>) em.createQuery(SPITTLES_BY_USERNAME).
        setParameter("username", spitter.getUsername()).
        getResultList();
  }
  

  public Spitter getSpitterByUsername(String username) {
    return (Spitter) em.createQuery(SPITTER_FOR_USERNAME).
        setParameter("username", username).
        getSingleResult();
  }
}

    第三步,在spitter-servlet.xml中添加下面这两个bean:
    <bean name="spitter/spittles" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="mediaTypes">
			<map>
				<entry key="htm" value="text/html"/>
				<entry key="json" value="application/json"/>
			</map>
		</property>
		<property name="defaultViews">
			<list>
				<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
			</list>
		</property>
</bean>

其中MappingJacksonJsonView的作用是将java对象映射为JSON格式资源的类,需要在类路径中添加jackson JSON库,ContentNegotiatingViewResolver的作用是根据客户端对返回资源格式的不同需求,配置相应的返回格式,默认的返回JSON格式的资源
    第四步,在eclipse的pyDev视图下使用python调用这个api,显示用户john所发的所有微博,代码如下:
#!/bin/env python

import httplib
import os, sys
import traceback

# Web Services API Utilities
from wsautils import *

GET  = 'GET'
POST = 'POST'
DELETE = 'DELETE'
PUT = 'PUT'

def executeRequest(method, uri, body=None):
    connection = httplib.HTTPConnection( 'localhost', 8090 )
    requestHeaders={'Content-Type': 'application/json', 'Accept': 'application/json'}
    connection.request( method, uri, body, requestHeaders )
    response = connection.getresponse()
    result   = Response( response.status, response.reason, response.getheaders(), response.read(), None )
    return result

#get a user's spittle(s)
result = executeRequest(GET, '/Spitter/spitters/john/spittles')
print result

这个例子中,在tomcat里设置服务器接受http请求的端口为8090,httplib是python内置的用于http调用的模块,wsautils是我自己的module,这里就引用了其中的Response类,来封装一个http response,大家也可以根据需要自己也一个这样的类,下面是运行结果:
Response Status  : 200
Response Reason  : OK
Response Headers : {'transfer-encoding': 'chunked', 'date': 'Fri, 04 Apr 2014 05:31:19 GMT', 'content-type': 'application/json', 'server': 'Apache-Coyote/1.1'}
Response Body    : [
{
  "text":"i live in beijing",
  "when":1393870571000,
  "id":1,
  "spitter":{
   "username":"john",
   "updateByEmail":true,
   "email":"xglvddz@gmail.com",
   "fullName":"john Donkey",
   "password":"19850115",
   "id":48180
  }
},
{
  "text":"today is sunny",
  "when":1396587047000,
  "id":2,
  "spitter":{
   "username":"john",
   "updateByEmail":true,
   "email":"xglvddz@gmail.com",
   "fullName":"john Donkey",
   "password":"19850115",
   "id":48180
  }
}
]
1
0
分享到:
评论
2 楼 kesun_shy 2014-08-27  
收藏。。过两天就试一下。
1 楼 qindongliang1922 2014-04-04  
不错             

相关推荐

Global site tag (gtag.js) - Google Analytics