簡體   English   中英

如何將 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>

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:

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>   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM