Micro-mvc与springmvc整合,使用springmvc的controller机制。
整合后Springmvc的controller只编写接口,参数名称必须用@RequestParam注解。
使用@InjectGroovy在接口中声明对应的groovy实现名称。
其他与传统springmvc的controller无异。
package foo.web;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nh.micro.service.InjectGroovy;
@Controller
@RequestMapping("test")
@InjectGroovy(name="TestController")
public interface TestController {
@RequestMapping("echo")
@ResponseBody
public Map echo(@RequestParam(value="str") String str,HttpServletRequest httpRequest);
}
Controller的实现groovy
package groovy;
import javax.servlet.http.HttpServletRequest;
import com.nh.micro.service.InjectGroovy;
import foo.service.TestService;
class TestController {
@InjectGroovy(name="TestService")
public TestService testService;
public Map echo(String str,HttpServletRequest httpRequest) {
System.out.println("this is controller proxy");
testService.test("111");
Map retMap=new HashMap();
retMap.put("status", "0");
return retMap;
}
}
配置包扫描
使用GroovyBeanScannerConfigurer代替context:component-scan对controller进行扫描。
<bean class="com.nh.micro.service.GroovyBeanScannerConfigurer">
<property name="scanPath" value="foo.web"></property>
</bean>
<!-- <context:component-scan base-package="foo.web" /> -->
测试
http://localhost:8080/micro-springmvc-demo/test/echo?str=111
注意:springmvc3.1版本需要引用jackson-all-1.8.1.jar。
否则可能出现异常 Could not find acceptable representation 406 (Not Acceptable)