jetty通过main 函数启动springmvc工程

背景:想调用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);
		
	}
}

 

启动Spring MVC通常涉及以下几个步骤: 1. **添加依赖**:在你的项目中,你需要在Maven或Gradle构建工具的pom.xml或build.gradle文件中添加Spring MVC相关的依赖,如`spring-web`和`spring-webmvc`。 2. **配置Spring容器**:在Spring XML配置文件(如applicationContext.xml或application-context.yml)中,设置Spring的核心配置,包括数据源、事务管理等,并创建DispatcherServlet的配置。例如,添加 `<servlet>` 和 `<context-param>` 配置Spring MVC的Servlet映射。 ```xml <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 3. **创建控制器**:创建一个实现了`@Controller`注解的类,用于处理HTTP请求。这些类包含处理请求的方法,可以接受GET、POST等请求并返回视图或数据。 4. **视图解析**:配置视图解析器,告诉Spring MVC如何查找和渲染JSP、Thymeleaf或其他模板引擎生成的HTML页面。 5. **启用监听器**:在web.xml中添加Spring MVC的监听器,如`<listener>`标签下的`ContextLoaderListener`,以便初始化Spring应用上下文。 6. **部署应用**:将项目部署到Tomcat、Jetty或其他Web服务器上,然后通过浏览器访问指定URL来测试Spring MVC应用程序是否正常启动
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值