简体   繁体   English

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

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

I have an existing Spring app that is a web application and I'm trying to add a REST API to it.我有一个现有的 Spring 应用程序,它是一个 web 应用程序,我正在尝试向它添加一个 REST API。

I'm not sure how to connect everything so it works我不确定如何连接一切以使其正常工作

I've added an entry to web.xml.我已将条目添加到 web.xml。 Originally the servlet class was pointed to my DispatcherServlet that I created, but I pointed it to org.springframework.web.servlet.DispatcherServlet based things I found online.最初 servlet class 指向我创建的 DispatcherServlet,但我将它指向 org.springframework.web.servlet.DispatcherServlet 基于我在网上找到的东西。

web.xml 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>

rest-servlet.xml 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>

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

any help to point me in the right direction would be appreciated任何帮助我指出正确方向的帮助将不胜感激

A copy past from a project:从一个项目复制过去:

web.xml 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 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/>

The answer to this is there was a filter active, so the /rest/ entrypoint needed to be added to it like this:答案是有一个过滤器处于活动状态,因此 /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>   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重构此 REST API Spring 启动应用程序? - How to refactor this REST API Spring Boot app? 如何将现有的 SOAP Web 服务调用到 REST API Z38008DD81C2F4D79185ECF6E0CE8AF1 启动应用程序 - How to invoke existing SOAP webservice into REST API Spring boot application 如何在 Spring Boot Rest API 中以 XML 形式返回对象列表 - How to return a list of objects as XML in Spring boot rest API 无法将 Spring HATEOAS 集成到我现有的 Spring Boot REST API 中 - Not able to integrate Spring HATEOAS in my existing Spring Boot REST API 返回 XML 响应 Rest API 和 Spring - Return XML response Rest API with Spring Xml 至 java rest Z8A5DA52ED126447D359E70C05AZ721A8A - Xml to java rest api (spring boot) 如何为基于Spring的Rest API和静态内容增加安全性 - How to add security to spring based rest api's and static content 如何在 Spring Boot Rest api 响应的 ResponseEntity 中添加自定义属性 - How to add custom attributes in ResponseEntity of Spring Boot Rest api response 如何在Activity App Ui中使用Spring Boot Activity Rest api - How to use Spring Boot Activity Rest api in Activity App Ui 如何在Spring中以编程方式将Java中的bean添加到app-context.xml中? - How to programatically add beans in Java to the app-context.xml in Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM