我有一个在 Eclipse 中构建的 Spring MVC 应用程序,我试图将其部署为战争(对 glassfish)。我有一个看起来像这样的应用程序类:
package com.jp5.rest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
@Service
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void init() {
SpringApplication.run(Application.class);
}
}
编辑: 我越来越接近了。战争文件现在部署。但是,我无法访问任何 Web 服务端点(它们都返回 404)现在,我有一个 web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
像这样的 application-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="application" class="com.jp5.rest.Application"
init-method="init" >
</bean>
<context:component-scan base-package="com.jp5.rest"/>
</beans>
控制器看起来像这样:
package com.jp5.rest;
@ComponentScan
@EnableAutoConfiguration
@Controller
@RequestMapping("/jp5/rest/message")
public class MessageRestService
{
@RequestMapping(method=RequestMethod.PUT, value="/test")
public @ResponseBody testResult test()
{
return new testResult(true, "test");
}
}
编辑 2 感谢您的指点。来自这里的解决方案:http: //spring.io/guides/gs/convert-jar-to-war/是添加这样的类。我还没有测试过,但是,我认为这可以代替 web.xml:
package com.jp5.rest;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.SpringBootServletInitializer;
public class Jp5RestXML extends SpringBootServletInitializer {
@Override
protected void configure(SpringApplicationBuilder application) {
application.sources(Application.class);
}
}
谢谢