背景:想调用springmvc提供的controller接口, 一般的操作为建立一个web项目, 通过web.xml文件的配置将springmvc纳入servlet容器。 本文使用jetty的方式,通过Java的main函数启动一个springmvc程序, 并可对外提供http访问能力
bug:客户端的请求现在可以发送到服务端,但服务端暂时收不到传递的参数,暂时没有找到原因
1. 所需的jar包
<!-- jetty jar包, 9.3版本可能需要jdk1.8 -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>9.2.19.v20160908</version>
</dependency>
<!-- json转换jar包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- springmvc所需jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
2. jetty通过main函数启动springmvc项目
package com.carl.jetty.server;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
* @Desc:
* @Auth: Carl
* @Time: 2018年8月26日 上午11:14:36
*/
public class RestServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletContextHandler handler = new ServletContextHandler();
// 服务器根目录,类似于tomcat部署的项目。 完整的访问路径为ip:port/contextPath/realRequestMapping
//ip:port/项目路径/api请求路径
handler.setContextPath("/api");
XmlWebApplicationContext context = new XmlWebApplicationContext();
//加载spring配置文件
context.setConfigLocation("classpath:spring-jetty.xml");
//相当于web.xml中配置的ContextLoaderListener
handler.addEventListener(new ContextLoaderListener(context));
//springmvc拦截规则 相当于web.xml中配置的DispatcherServlet
handler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
server.setHandler(handler);
server.start();
server.join();
}
}
3. controller编写
package com.carl.jetty.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @Desc:
* @Auth: Carl
* @Time: 2018年8月26日 上午11:28:06
*/
@RestController
、、这里特意让程序报错,让你更快的注意到这里。 注意这里的请求没有/api 开头, 最终调用时有api那是在server中设置的上下文路径。
@RequestMapping("/user/")
public class UserController {
@RequestMapping(value="getUserInfo", method=RequestMethod.POST)
public String userInfo(String userId) {
return "userInfo=[userId-test="+userId+"]";
}
}
4. http请求调用springmvc提供的接口
package com.carl.jetty.server;
import org.springframework.web.client.RestTemplate;
/**
* @Desc:
* @Auth: Carl
* @Time: 2018年8月26日 上午11:22:56
*/
public class RestClient {
private static RestTemplate restTemplate = new RestTemplate();
public static void main(String[] args) {
String userId = "1";
//bug: 程序已经调通了, 但参数传不到对应的请求, 后期再看这个问题
//访问路径为Server设置的上下文路径+controller的请求路径
String result = restTemplate.postForObject("http://localhost:8080/api/user/getUserInfo", userId, String.class);
System.out.println(result);
}
}