[英]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.