英文:
How to create a REST endpoint in Spring Boot Java without @SpringBootApplication, using only XML file?
问题
我正在尝试在两个非 Web 应用程序之间实现一个 REST 端点,并且所有的配置都在 XML 文件中。
我已经创建了一个简单的控制器,其中有一个方法,只返回 "OK",这样我就可以使用 Postman 运行一些测试。
不幸的是,端点没有被创建。
我进行了一些研究,发现我需要添加 "context" 标签,并将 component-scan 指向控制器所在的包,以使其工作。
但是,我的当前实现还不足以使其工作:
<context:component-scan base-package="com.app.REST"/>
我的控制器类如下:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping("/test")
@ResponseBody
public String test(){
return "OK";
}
}
我的问题是:是否有一种方法可以在不对主类使用 @SpringBootApplication
注解的情况下创建一个 REST 端点?如果可以,我漏掉了什么?是我的 XML 文件还是其他地方出了问题?
英文:
I'm trying to implement a REST endpoint between two non-web applications with all configuration in XML files.
I've created a simple controller with a method that only returns "OK"
, so I can run some tests using Postman.
Unfortunately, the endpoint is not being created.
I did some research and I found out that I need to add the "context" tag with the component-scan pointing to the controller package for it to work.
But my current implementation is not enough for it to work:
<context:component-scan base-package="com.app.REST"/>
My controller class is:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
@RequestMapping("/test")
@ResponseBody
public String test(){
return "OK";
}
}
My question is: is there any way to create a REST endpoint without annotating the main class with @SpringBootApplication
? If yes, what am I missing? Is it something in my XML file or somewhere else?
答案1
得分: 1
为了启用MVC功能,您需要指示Spring扫描您的控制器,这可以通过<mvc:annotation-driven />
标签来完成。
由于DispatcherServlet负责处理您的请求,您需要在web.xml中为其添加正确的配置:
...
<servlet>
<servlet-name>my-dispatcher-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:web-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
LE:
<mvc:annotation-driven>
类似于 <context:component-scan>
,它们都注册带有 @Controller、@Component、@Service、@Repository 和 @Bean 注解的所有 bean...
mvc:annotation-driven
的主要区别在于它还创建了一些额外的 bean,这些 bean 负责在 dispatcherServlet 中注册端点(HandlerMapping、HandlerAdapters 以及一些默认的 conversionServices,用于表示在控制器中接收的数据,例如)。
英文:
For enabling the MVC functionality you need to instruct spring to scan for your controllers
this is done via the <mvc:annotation-driven />
tag.
Also as the DispatcherServlet is taking care of your requests you need to add the correct configuration for it in your web.xml
....
<servlet>
<servlet-name>my-dispatcher-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:web-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
LE:
<mvc:annotation-driven>
is similar to <context:component-scan>
they register all beans annotated with @Controller, @Component, @Service, @Repository and @Bean...
The main difference with mvc:annotation-driven
is that it also creates some extra beans which take care of the endpoint registration in the dispatcherServlet (the HandlerMapping, HandlerAdapters and some default conversionServices needed to represent your data received in your controllers for example)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论