如何将 Spring REST API 添加到使用 spring xml 的现有应用程序

How to add a Spring REST API to an existing app that uses spring xml

我有一个现有的 Spring 应用程序,它是一个 Web 应用程序,我正在尝试向其添加 REST API。

我不确定如何连接一切才能正常工作

我已将条目添加到 web.xml。最初 servlet class 指向我创建的 DispatcherServlet,但我将它指向 org.springframework.web.servlet.DispatcherServlet 基于我在网上找到的东西。

web.xml

    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

休息-servlet.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <context:component-scan base-package="com.company.platform.rest" />
    <mvc:annotation-driven />
</beans>

Class:

package com.company.platform.rest;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest")
public class RestDispatcherServlet {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/service/greeting")
    public GreetingTest greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new GreetingTest(counter.incrementAndGet(), String.format(template, name));
    }
}

如能为我指明正确的方向,我们将不胜感激

项目中的复制粘贴:

web.xml

<servlet>
        <servlet-name>rest-api</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest-api</servlet-name>
        <url-pattern>/API/*</url-pattern>
    </servlet-mapping>

rest-api-servlet.xml

<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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:security="http://www.springframework.org/schema/security"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security-4.2.xsd">

    <mvc:annotation-driven/>

答案是有一个过滤器处于活动状态,因此需要像这样向其中添加 /rest/ 入口点:

<filter>
        <filter-name>SomeFilter</filter-name>
        <filter-class>com.SomeFilter</filter-class>
        
        <init-param>
            <param-name>entryPoints</param-name>
            <param-value>/someform.form,
                        /somejavascript.js
                        /rest/*
            </param-value>
        </init-param>
    </filter>