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>

1 Answer 1

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.