论坛首页 Java企业应用论坛

自己动手写淘宝开放平台:Rop(将WebService REST进行到底!!)

浏览 99904 次
该帖已经被评为精华帖
作者 正文
   发表时间:2012-03-14  
陈先生太牛了
0 请登录后投票
   发表时间:2012-03-16   最后修改:2012-03-16
貌似春暖花开 写道
框架搭建的挺好。
个人觉得如下几个方面可有改进的地方

1. 和spring mvc框架绑定。
   很多基于Spring框架的web应用,并不采用spring mvc框架,因此就没有必要加载spring mvc相关的lib等。可以采用filter方式来替换spring mvc的拦截器。

2. Service的入口 url
可以增加相关的annotation, 采用struts2 的方式, 一个service 中的多个方法  采用url_method 方式 即是 url 对应servcie类,method 对应相关的方法,比如 CalculateService

@UrlMapping(“/operation/CalculateService”)
public class CalculateService{
@ServiceMethod(“add”)
public int add(int a, int b)….
@ServiceMothod(“multiply”)
Public int multiply(int a, int b)…

对应请求的url   /operation/CalculateService_add
or /operation/CalculateService_multiply


3. 开发人员功能扩展接口
比如像校验这样的功能,光靠annotation 是不够的,比如请求修改数据库记录的restful接口,校验应该包括数据是否在数据库中存在等等,因此应该能方便插入相关的功能。
   因此应该采用一些相关的设计模式,比如 责任链模式,在请求真正达到 restful servce之前,开发人员可以对request 或者 response进行相应的处理,也可以直接返回错误结果等。比如有个 IP 限定功能,那么开发人员提供一个 校验的handle, 如果不符合,择直接返回错误给 调用者,这样请求根本都不会到达真正的resutful service 中。
可以采用 axia2的链式处理,我觉得它能很好解决这样的问题

以上建议仅作参考

    建议非常好!这也说明开源是发展项目的源动力。
1. 1建议我会融入到项目中,其实脱离Spring MVC是完全可以做到了,这个依赖性就低了很多。
2. 这点,如果是按照TOP的方式,URL是固定的,只是参数是变化了,个人觉得所有服务的URL固定的好处还是好过URL各异的方式,因为每个服务只要关心业务级参数即可,但是固定的URL应该是可以设定的,不过不应局限为/router,这个统一的URl可以有变化。
3. 校验我是基于JSR303 JSR303是可以扩展了,象完成数据库相关的校验也很容易扩展做到,所以这部分应该没有必要再搞一个校验扩展出来,这样框架的东西就多了,直接利用JSR303的扩展会更简单些。
0 请登录后投票
   发表时间:2012-03-18  
shenjc2008 写道
sky_cn 写道
shenjc2008 写道
总是报这个警告:warning no match for this type name: within [Xlint:invalidAbsoluteTypeName]

2012-03-08 20:18:47,140  INFO [main] (AbstractHandlerMethodMapping.java:188) - Mapped "{[/sol],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.rop.impl.AnnotationRopServiceRouter.test()
2012-03-08 20:18:47,141  INFO [main] (AbstractHandlerMethodMapping.java:188) - Mapped "{[/router],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.rop.impl.AnnotationRopServiceRouter.service(org.springframework.web.context.request.WebRequest,javax.servlet.http.HttpServletResponse)

而且里面的没有一个值,不知道怎么回事

访问接口总是显示:

-<error solution="服务目前无法使用" message="服务不可用" code="1"> -<subErrors> -<subError> <code>isv.sample-user-add-service-unavailable</code> <message>调用后端服务sample.user.add抛异常,服务不可用</message> </subError> </subErrors> </error>


你服务应该没有部署成功吧



搞清楚了,是我没传appKey等参数的缘故,只是楼主的代码还没判断是否有appKey等参数,所以并没有提示缺少appKey,看来楼主的代码还没写完...



这个问题。。。我也出现了。。跟appKey有关系吗?你是怎么解决的。。我也是一直报输出

<error solution="服务目前无法使用" message="服务不可用" code="1"> -<subErrors> -<subError> <code>isv.sample-user-add-service-unavailable</code> <message>调用后端服务sample.user.add抛异常,服务不可用</message> </subError> </subErrors> </error>
0 请登录后投票
   发表时间:2012-03-19  
to:shenjc2008及各位亲:
  不好意思,上次改后漏了两个文件,rop-servlet.xml和web.xml,现在应该可以了,请大家pull一下。
0 请登录后投票
   发表时间:2012-03-19   最后修改:2012-03-19
原来我都是用svn做版本管理,也没有用过maven,这两天抱佛脚中...项目马上做如下调整:
  1.完全使用maven管理;
  2.使用servlet 3.0,将web.xml代码化,以便可以将rop做成一个jar包;(当然就只能支持servlet 3.0)了,这点适合否? 
  3.测试集成jetty,这样客户端IDE就不要设置tomcat和测试环境了,避免客户端的环境差异。
  欢迎大家给一些建议,谢谢!
0 请登录后投票
   发表时间:2012-03-19  
javalinjx 写道
shenjc2008 写道
sky_cn 写道
shenjc2008 写道
总是报这个警告:warning no match for this type name: within [Xlint:invalidAbsoluteTypeName]

2012-03-08 20:18:47,140  INFO [main] (AbstractHandlerMethodMapping.java:188) - Mapped "{[/sol],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.rop.impl.AnnotationRopServiceRouter.test()
2012-03-08 20:18:47,141  INFO [main] (AbstractHandlerMethodMapping.java:188) - Mapped "{[/router],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.rop.impl.AnnotationRopServiceRouter.service(org.springframework.web.context.request.WebRequest,javax.servlet.http.HttpServletResponse)

而且里面的没有一个值,不知道怎么回事

访问接口总是显示:

-<error solution="服务目前无法使用" message="服务不可用" code="1"> -<subErrors> -<subError> <code>isv.sample-user-add-service-unavailable</code> <message>调用后端服务sample.user.add抛异常,服务不可用</message> </subError> </subErrors> </error>


你服务应该没有部署成功吧



搞清楚了,是我没传appKey等参数的缘故,只是楼主的代码还没判断是否有appKey等参数,所以并没有提示缺少appKey,看来楼主的代码还没写完...



这个问题。。。我也出现了。。跟appKey有关系吗?你是怎么解决的。。我也是一直报输出

<error solution="服务目前无法使用" message="服务不可用" code="1"> -<subErrors> -<subError> <code>isv.sample-user-add-service-unavailable</code> <message>调用后端服务sample.user.add抛异常,服务不可用</message> </subError> </subErrors> </error>




你可能跟我一开始一样,想通过浏览器测试是吧?为图方便,所以很多参数都没有传递,结果报上述错误。
解决办法:如果想通过浏览器测试,把参数传全就可以了。或者使用楼主的测试用例test一下,你就发现了
0 请登录后投票
   发表时间:2012-03-19   最后修改:2012-03-19
stamen 写道
原来我都是用svn做版本管理,也没有用过maven,这两天抱佛脚中...项目马上做如下调整:
  1.完全使用maven管理;
  2.使用servlet 3.0,将web.xml代码化,以便可以将rop做成一个jar包;(当然就只能支持servlet 3.0)了,这点适合否? 
  3.测试集成jetty,这样客户端IDE就不要设置tomcat和测试环境了,避免客户端的环境差异。
  欢迎大家给一些建议,谢谢!

 

jetty 的   

        
<build><finalName>mvc3</finalName>  
        <plugins>  
            <!-- jetty插件 -->  
            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>maven-jetty-plugin</artifactId>  
                <version>6.1.5</version>  
                <configuration>  
                    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>  
                    <scanIntervalSeconds>3</scanIntervalSeconds>  
                    <contextPath>/mvc3</contextPath>  
                    <webDefaultXml>jetty.xml</webDefaultXml>  
                    <jettyEnvXml>jetty-env.xml</jettyEnvXml>  
                    <connectors>  
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                            <port>9088</port>  
                        </connector>  
                    </connectors>  
                </configuration>  
            </plugin>           
              
        </plugins>  
          
    </build>  

 tomcat   

 

 

	<build>		
<plugins>
			
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${java-version}</source>
					<target>${java-version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>install</id>
						<phase>install</phase>
						<goals>
							<goal>sources</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>aspectj-maven-plugin</artifactId>
				<!-- Have to use version 1.2 since version 1.3 does not appear to work 
					with ITDs -->
				<version>1.2</version>
				<dependencies>
					<!-- You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
					<dependency>
						<groupId>org.aspectj</groupId>
						<artifactId>aspectjrt</artifactId>
						<version>${org.aspectj-version}</version>
					</dependency>
					<dependency>
						<groupId>org.aspectj</groupId>
						<artifactId>aspectjtools</artifactId>
						<version>${org.aspectj-version}</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>test-compile</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<outxml>true</outxml>
					<source>${java-version}</source>
					<target>${java-version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
				
			</plugin>
		</plugins>
	</build>
0 请登录后投票
   发表时间:2012-03-19  
shenjc2008 写道


你可能跟我一开始一样,想通过浏览器测试是吧?为图方便,所以很多参数都没有传递,结果报上述错误。
解决办法:如果想通过浏览器测试,把参数传全就可以了。或者使用楼主的测试用例test一下,你就发现了



不是的,之前就是用例子的,之前是由于web.xml配置有问题,所以出现的,现在重新pull解决了。。谢谢啊,哈哈!
0 请登录后投票
   发表时间:2012-03-20  
sky_cn 写道
stamen 写道
原来我都是用svn做版本管理,也没有用过maven,这两天抱佛脚中...项目马上做如下调整:
  1.完全使用maven管理;
  2.使用servlet 3.0,将web.xml代码化,以便可以将rop做成一个jar包;(当然就只能支持servlet 3.0)了,这点适合否? 
  3.测试集成jetty,这样客户端IDE就不要设置tomcat和测试环境了,避免客户端的环境差异。
  欢迎大家给一些建议,谢谢!

 

jetty 的   

        
<build><finalName>mvc3</finalName>  
        <plugins>  
            <!-- jetty插件 -->  
            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>maven-jetty-plugin</artifactId>  
                <version>6.1.5</version>  
                <configuration>  
                    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>  
                    <scanIntervalSeconds>3</scanIntervalSeconds>  
                    <contextPath>/mvc3</contextPath>  
                    <webDefaultXml>jetty.xml</webDefaultXml>  
                    <jettyEnvXml>jetty-env.xml</jettyEnvXml>  
                    <connectors>  
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                            <port>9088</port>  
                        </connector>  
                    </connectors>  
                </configuration>  
            </plugin>           
              
        </plugins>  
          
    </build>  

 tomcat   

 

 

	<build>		
<plugins>
			
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>${java-version}</source>
					<target>${java-version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>install</id>
						<phase>install</phase>
						<goals>
							<goal>sources</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>aspectj-maven-plugin</artifactId>
				<!-- Have to use version 1.2 since version 1.3 does not appear to work 
					with ITDs -->
				<version>1.2</version>
				<dependencies>
					<!-- You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
					<dependency>
						<groupId>org.aspectj</groupId>
						<artifactId>aspectjrt</artifactId>
						<version>${org.aspectj-version}</version>
					</dependency>
					<dependency>
						<groupId>org.aspectj</groupId>
						<artifactId>aspectjtools</artifactId>
						<version>${org.aspectj-version}</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>test-compile</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<outxml>true</outxml>
					<source>${java-version}</source>
					<target>${java-version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
				
			</plugin>
		</plugins>
	</build>

 

哈哈,谢谢,发现开源的好处就是多啊!已经现成的了!,容我再摸一下Maven,不至于在没有理解Maven的前提误用了Maven,这时先谢谢sky_cn了!

0 请登录后投票
   发表时间:2012-03-20   最后修改:2012-03-20

陈老师不用过于客气,另外今天发现陈老师又重构了代码了

在web.xml上面加入了

 

 

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:com/sample/applicationContext.xml
        </param-value>
    </context-param>
 

之前跑老师的demo也遇到这个问题,我偷懒的方式是直接在rop-servlet.xml里面加入了

 

<context:component-scan base-package="com.sample">
		<!-- 假设所有的服务方法都放在rop包下 -->
		<context:include-filter type="aspectj"	expression="within(com.sample.rop..*)" />
	</context:component-scan>
 

那样可以少写个配置文件,O(∩_∩)O~

 

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics