struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rich P Peters, II (JIRA)" <>
Subject [jira] [Created] (WW-4588) Improve the Struts2 Rest plugin to honor Accept nad Content-Type headers
Date Thu, 14 Jan 2016 16:09:39 GMT
Rich P Peters, II created WW-4588:

             Summary: Improve the Struts2 Rest plugin to honor Accept nad Content-Type headers
                 Key: WW-4588
             Project: Struts 2
          Issue Type: Improvement
          Components: Plugin - REST
    Affects Versions: 2.3.24
         Environment: tomcat
            Reporter: Rich P Peters, II

Currently the rest plugin primarily looks at the url extension to connect the proper content
type handlers.  This should be extended to use the Accept and Content-Type headers to drive
the content-type handlers when no extension is present.  Note that the plugin currently partially
handles the case for input request using the content-type header, but can be overridden by
the extension.

In a use case,  if a request specifies in the header that the Content-Type is application/json,
the input request data is converted to the json data format. If a request specifies the Accept:application/json
header ,  the output data will be converted to json.  This is cleaner than specifying the
extension in a lot of cases where the application rest urls have to be generated in javascript.

a short unit test shows how the accept header is ignored:
    public void testObtainingHandlerForResponseByAcceptHeader() throws Exception {

        // given
        final DefaultContentTypeHandlerManager handlerManager = new DefaultContentTypeHandlerManager();
        handlerManager.setContainer(new DummyContainer("application/json", "json"));

        MockHttpServletRequest request = new MockHttpServletRequest();

        final MockHttpServletResponse response = new MockHttpServletResponse();

        // when
        ContentTypeHandler handler = handlerManager.getHandlerForResponse(request,response);

        // then
        assertEquals("application/json", handler.getContentType());
        assertEquals("json", handler.getExtension());
the output show the failure to acquire a handler:
junit.framework.AssertionFailedError: null

	at junit.framework.Assert.assertTrue(
	at junit.framework.Assert.assertNotNull(
	at junit.framework.Assert.assertNotNull(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at junit.framework.TestCase.runTest(
	at junit.framework.TestCase.runBare(
	at junit.framework.TestResult$1.protect(
	at junit.framework.TestResult.runProtected(
	at junit.framework.TestSuite.runTest(
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
	at com.intellij.rt.execution.junit.JUnitStarter.main(

This message was sent by Atlassian JIRA

View raw message