Skip to content

@ResponseBody with returned String results in unresolvable view ServletException [SPR-6148] #10816

@spring-projects-issues

Description

@spring-projects-issues

Andrew Ebaugh opened SPR-6148 and commented

We are using the @ResponseBody similar as indicated in the reference doc, like:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
return "Hello World";
}

The returned String does get written to the response by the StringHttpMessageConverter (and the remote client is happy), but then the String goes on to be rendered as an InternalResourceView. Since the strings we are returning are not valid as internal resources, we see a ServletException that is, among other things, caught by our HandlerInterceptor.

One reason this is a problem is because we are mixing together some responses that are marshalled to XML, some that are returned as a strings via the ResponseBody, and others that are valid InternalResourceViews.

One approach may be that AnnotationMethodHandlerAdapter should return null instead of attempting to create a ModelAndView after this condition is met:

AnnotationMethodHandlerAdapter.getModelAndView(..) :
if (returnValue != null && handlerMethod.isAnnotationPresent(ResponseBody.class)) {
handleRequestBody(returnValue, webRequest);
}

As it is, it proceeds to:

if (returnValue instanceof String) {
return new ModelAndView((String) returnValue).addAllObjects(implicitModel);
}


Affects: 3.0 M4

Attachments:

Referenced from: commits e49869e

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions