Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per-Controller MessageConverter [SPR-7213] #11872

Closed
spring-projects-issues opened this issue May 18, 2010 · 4 comments
Closed

Per-Controller MessageConverter [SPR-7213] #11872

spring-projects-issues opened this issue May 18, 2010 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Tyler Vallillee opened SPR-7213 and commented

I would like to know whether there is a way to specify the order of message converters on a per-controller basis.

For example, let's assume I have the following POJO:

@XmlRootElement("foo")
public class Foo {

   private String messageText;
   private Integer messageId;

... //getters and setters skipped
}

And I have the following controllers. First, a controller whose clients expect XML to be returned:

@Controller
public MyXmlController {

	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public @ResponseBody Foo getFooById(@PathVariable int id) {
		
		Foo foo = myService.getFooById(id);
		return foo;
	}
}

and now a controller whose clients expect JSON back:

@Controller
public MyJsonController {

	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public @ResponseBody Foo getFooForUser(@PathVariable int id) {
		
		Foo foo = myService.getFooByUser(id);
		return foo;
	}
}

Now in my servlet.xml file, I have the following to handle @ResponseBody marshalling:

    <bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <util:list>
            
                <!-- Support XML marshaling -->
                <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                    <property name="marshaller" ref="xmlMarshaller"/>
                    <property name="unmarshaller" ref="xmlMarshaller"/>
                </bean>

                <!-- Support JSON --> 
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
                
            </util:list>
        </property>
    </bean>

The limitation here is that, given a HTTP request with an "Accept: *" header (as is typical from most web browsers) the data from MyJsonController will always be returned as XML as the XML marshaller is the first listed messageConverter for the AnnotationMethodHandlerAdapter.

Is there a way I can specify a different ordered messageConverter list that the JsonController could reference?

I am aware of ContentNegotiatingViewResolver, however in my case the API paths are legacy and difficult to update.

It would be ideal if my controller could give a hint to the MessageConverter it prefers, or if it could have its own preferred list of MessageConverters


Affects: 3.0.2

Reference URL: http://forum.springsource.org/showthread.php?p=299622

@spring-projects-issues
Copy link
Collaborator Author

Tyler Vallillee commented

Didn't mean for the above request to appear as a question. It is a feature request to specify message converters on a per-controller basis. See the forum thread for more details.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 11, 2011

Rossen Stoyanchev commented

Would #12012/SPR-7354 address the use case you have in mind?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 11, 2011

Tyler Vallillee commented

Yes, and it sounds like a better solution due to adoption of JSR standards. Please close this request if the team proceeds with #12012/4

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

The produces @RequestMapping condition introduced in Spring 3.1 addresses this issue.

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.1 M2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants