0

I have a controller loader outside from my Spring Web MVC Project.

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

Let us create an instance of it :

Object myControllerClassInstance = myControllerClass.newInstance();

then I use the below code snippet to register the endpoint in current running tomcat server (where my current Spring MVC project is already running)

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

Result successfully registering endpoint with message

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

Where signature in UserDeployedRestServiceController was :

    @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());

Now I am trying to hit the URL from outside with json data as request body but getting

We got httpEntity body = null

I have tried with @ResponseBody as well but no luck + I have also tried with

requestMappingHandlerMapping.setDefaultHandler(new UserDeployedServiceRequestHandler());

where

    class UserDeployedServiceRequestHandler implements HttpRequestHandler {

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

Please tell me what exactly I am missing. Why request body is always null or empty ?

My application-context is having

 <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>
Surajit Biswas
  • 649
  • 5
  • 20

1 Answers1

0

I think your issue may be as simple as the fact that GET requests in general don't support request body. See this SO question: HTTP GET with request body

FWIW - I have this working using basically

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

and

public class ControllerClass {
    public ResponseEntity<ResponseType> controllerMethod (@RequestBody RequestType request) {
        // service request
        return new ResponseType();
    }
}
pedorro
  • 2,613
  • 20
  • 24