简体   繁体   中英

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.

I'm not sure how to connect everything so it works

I've added an entry to 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.

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

any help to point me in the right direction would be appreciated

A copy past from a project:

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

The answer to this is there was a filter active, so the /rest/ entrypoint needed to be added to it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM