java - RequestMappingHandlerMapping registerMapping 未与 RequestBody/HttpEntity 绑定(bind)

标签 java spring-mvc request-mapping

我的 Spring Web MVC 项目外部有一个 Controller 加载器。

喜欢:Class myControllerClass = classLoader.loadClass("withfully.packagename.className");

让我们创建它的一个实例:

Object myControllerClassInstance = myControllerClass.newInstance();

然后我使用下面的代码片段在当前运行的 tomcat 服务器中注册端点(我当前的 Spring MVC 项目已经在运行)

    private void addMapping(String urlPath, RequestMethod methodType, String methodName, String consumes,
        String produces) {

    RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths("/someCommon/test1/test2")
.methods(RequestMethod.GET)
.consumes("application/json")
.produces("application/json")
.build();

    for (Method m : myControllerClass.getDeclaredMethods()) {
        m.setAccessible(true);
        if (m.getName().equals(methodName)) {
            requestMappingHandlerMapping.registerMapping(requestMappingInfo, myControllerClassInstance, m);
        }
    }
}

结果成功使用消息注册端点

INFO: Mapped "{[/someCommon/test1/test2],methods=[GET],consumes=[application/json],produces=[application/json]}" onto public test.testclass.process.Process userDefinedpackage.someCommon.controller.UserDeployedRestServiceController.someMethod(org.springframework.http.HttpEntity<java.lang.String>) throws java.lang.Exception

何处签名 UserDeployedRestServiceController是:

    @RequestMapping(value="test1/test2",method=RequestMethod.GET, consumes="application/json", produces="application/json")
    public @ResponseBody test.testclass.process.Process someMethod(HttpEntity<String> httpEntity) throws Exception{

System.out.println("We got httpEntity body = "+httpEntity.getBody());

现在我尝试使用 json 数据作为请求正文从外部访问 URL,但得到

我们得到了 httpEntity body = null

我尝试过@ResponseBody也一样,但没有运气+我也尝试过

requestMappingHandlerMapping.setDefaultHandler(new UserDeployedServiceRequestHandler());

哪里

    class UserDeployedServiceRequestHandler implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setStatus (200);
    }
}

请告诉我到底缺少什么。为什么请求正文总是为 null 或为空?

我的应用程序上下文有

 <mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
    </mvc:annotation-driven>

最佳答案

我认为您的问题可能很简单,因为 GET 请求通常不支持请求正文。看到这个问题:HTTP GET with request body

FWIW - 我基本上使用这个工作

RequestMappingInfo mappingInfo = RequestMappingInfo
            .paths("/the/url")
            .methods(RequestMethod.POST)
            .consumes(MediaType.APPLICATION_JSON_VALUE)
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .build();

Method meth = ControllerClass.class.getMethod("controllerMethod", RequestType.class));
mappingHandler.registerMapping(mappingInfo, controller, meth);

public class ControllerClass {
    public ResponseEntity<ResponseType> controllerMethod (@RequestBody RequestType request) {
        // service request
        return new ResponseType();
    }
}

关于java - RequestMappingHandlerMapping registerMapping 未与 RequestBody/HttpEntity 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57254813/

相关文章:

java - Java创建目录的问题

spring-mvc - 在Spring MVC上使用Servlet 3.0的MultipartConfig

java - 空 @PathVariable 会导致 ArgumentMismatchException

java - 如何在 spring 中将所有请求映射到单个 Controller 方法?

java - java中的递归与意外的输出

Java继承奇怪的行为

java - LibGDX Sprite 屏幕未加载图形或加载屏幕

Spring MVC 使用表单 :checkbox to bind data

java - 如何在表单提交之间保留 ModelMap?

spring-boot - Spring Boot/Java 将枚举值映射到 RequestParam