How to create a REST endpoint in Spring Boot Java without @SpringBootApplication, using only XML file?

huangapple go评论73阅读模式
英文:

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 &quot;OK&quot;, 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:

&lt;context:component-scan base-package=&quot;com.app.REST&quot;/&gt;

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(&quot;/test&quot;)
    @ResponseBody
    public String test(){
        return &quot;OK&quot;;
    }

}

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 &lt;mvc:annotation-driven /&gt; tag.

Also as the DispatcherServlet is taking care of your requests you need to add the correct configuration for it in your web.xml

....
&lt;servlet&gt;
        &lt;servlet-name&gt;my-dispatcher-servlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
            &lt;param-value&gt;classpath:web-config.xml&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;dispatcher-servlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

LE:

&lt;mvc:annotation-driven&gt; is similar to &lt;context:component-scan&gt; 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)

huangapple
  • 本文由 发表于 2020年9月1日 22:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63690083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定