当前位置: 首页 > 知识库问答 >
问题:

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

金伟
2023-03-14

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

我不知道如何连接所有东西,使其工作

我在网上添加了一个条目。xml。最初,servlet类指向我创建的DispatcherServlet,但我将其指向org。springframework。网状物servlet。我在网上找到的基于DispatcherServlet的东西。

网状物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>

rest-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>

类:

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));
    }
}

如果能给我指出正确的方向我将不胜感激

共有1个答案

马正初
2023-03-14

项目的副本:

网状物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>

RESTAPI 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/>
 类似资料:
  • 我很难知道如何将我的JFXPanel(它是一个mp3播放器)集成到一个现有的Swing应用程序中,该应用程序是一个JFrame,其中添加了几个不同的JPanel。我对JavaFX是一个完全的noob,它与Swing非常不同。 我能够得到下面的代码来运行和做我想做的事情。它将播放mp3文件从一个目录,我有他们包含在。这是某人编写的示例中的代码。我的问题是,这段代码创建了JFrame,有main方法和

  • 问题内容: 我一直在开发React应用程序,现在我需要Redux来处理它的某些方面。 阅读了一堆教程之后,我相当着迷于如何使“更智能”的组件“笨拙”以及如何将功能移入我的动作和化简器中。 因此,例如,应用程序的一个方面更多是待办事项列表样式。 我的课程之一是这样开始的: 因此,如您所见,这是非常繁重的逻辑。我已经开始通过在索引文件中添加来添加Redux ,并制作了一个基本的reducers文件,该

  • 我想在我的应用程序中添加带有,但一旦我将插件添加到我的应用程序中。yaml,我的应用程序不再运行。下面是错误 失败:生成失败,出现异常。 > 错误:任务执行失败:app:transformDexArchiveWithExternalLibsDexMergerForDebug'。 通用域名格式。Android建设者德兴。DexArchiveMergerException:合并dex存档时出错:/Us

  • 我想编写一个使用azure power shell运行的脚本来自动添加Web应用程序配置 蔚蓝的 它看起来像key="value" 我写这个脚本 但我知道上面的脚本只是获取我的Web应用程序,现在我需要访问MyWebApp 编辑:该脚本可以自动创建新的web应用程序并向其添加应用程序设置:

  • 我有一个工作应用程序,我想添加语音命令。当前应用程序定期(定时器)通过蓝牙来回传输数据。用户可以按按钮和NumberPickers来修改通过蓝牙发送的数据。还有从蓝牙链接接收到的数据,并显示在文本视图中。此应用程序目前工作正常。 我想做的是添加语音命令功能,以便用户可以选择按下按钮/数字选择器,或者可以仅使用语音命令更改值。 我测试了一些可以在各种网站上找到的从语音到文本的例子。我成功地测试了一个

  • 背景: 我正在使用Eclipse Lunar和谷歌应用程序引擎插件来尝试建立一个网站。一切都进行得很顺利,直到我想保存一些数据,所以我遵循了使用Objectify的教程。我下载了Objectify jar并将其放入“/war/WEB-INF/lib/”中,然后将其添加到类路径中。然后我做了一个类,并创建了几个我自己的测试类(现在可以假设这些类不包含错误)。我也可以成功部署。 问题 当试图加载使用O