spring - RestyGWT- Spring Rest 服务的自定义 url 映射

标签 spring rest spring-mvc gwt resty-gwt

我遇到的问题是我的 Spring Rest Controller 的映射方式与 RestyGWT 想要的方式不同。 我的申请地址是:http://localhost:8080/restgwt/

根据web.xml:

<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/action-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

我的 Spring 服务/ Controller 监听: http://localhost:8080/restgwt/service/test

但是我的 RestyGWT 服务调用此网址: http://localhost:8080/restgwt/restgwt/test

而且我不知道如何告诉 RestyGWT 进行更改 url 。请帮忙。

我知道最简单的解决方案是在 web.xml 中进行更改文件 servlet url-pattern参数

来自:<url-pattern>/service/*</url-pattern>

至:<url-pattern>/restgwt/*</url-pattern>

但我想让 RestyGWT 改变它的行为。


这里粘贴一些附加代码:

GWT 端的 TestService

package pl.korbeldaniel.restgwt.client;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.RestService;

public interface TestService extends RestService {
    @GET
    @Path("test")
    public void getInfo(MethodCallback<TestPojo> test);
}

Spring端的TestService

package pl.korbeldaniel.restgwt.server;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController()
public class TestService {
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public @ResponseBody TestEntity getInfo() {
        TestEntity test = new TestEntity();
        System.out.println("Hit server for getting _1");
        return new TestEntity();
    }
}

最佳答案

Reffering to the official documentation:

Configuring service root URLs There are two ways to configure service root URLs which are appended with the @Path annotation property when building the final service URL. For single service root URL the Defaults.setServiceRoot(String) method can be used. When several services with different service roots are used the @Options annotation is equipped with the serviceRootKey property which can be set to read service root entries provided with the static ServiceRoots.add(String, String) method.

Defaults.setServiceRoot(new Resource( GWT.getModuleBaseURL()).resolve("../rest").getUri());

所以我的 RestyGWT 的 REST 路径变为 http://domain-name/myGwtAppModuleName/rest/furtherPath 其中 futurePath 是 javax.ws.rs @Path(..) 值 将该行直接放入 GIN ClientModule 失败,并出现 java.lang.UnsatisfiedLinkError: com.google.gwt.core.client.impl.Impl.getModuleBaseURL()Ljava/lang/String 为了避免错误,我将其包装起来

    public class ClientModule extends AbstractPresenterModule {
        @Override
        protected void configure(){
           //your installs and binds here
           bind(RestyGwtConfig.class).asEagerSingleton();
        }
    }

    public class RestyGwtConfig {
    static {
    Defaults
.setServiceRoot(new Resource( GWT.getModuleBaseURL()).resolve("../rest").getUri());
    }
    }

关于spring - RestyGWT- Spring Rest 服务的自定义 url 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34431170/

相关文章:

json - 在 python 中创建一个没有 Flask 的 REST API

java - 前缀 "redirect:"无法解析 View

java - Spring AOP - 带有注释的每个方法的切入点

java - Spring hibernate : @Transaction (REQUIRES_NEW) not catching exception

java - Spring MockMvc : match a collection of JSON objects in any order

java - 如何使用 Java/Spring 将视频流式传输到浏览器?

java - 通过 REST API 将附件添加到 Jira

java - Jersey 在解析损坏的请求时抛出 NullPointerException

java - 如何在Spring中动态翻译Enum?

unit-testing - Spring MVC 4.2 : How to Unit Test Controller with @RequestPart Params