我正在使用spring 4 @RestController
,根据spring doc,这相当于@Controller
+ @ResponseBody
,所以现在 ALL 我的方法controller返回一些JSON对象。
但我只想排除一两种方法,我希望它们能够返回普通视图。但是,现在spring只会将文字字符串admin/msg/msg_index
发送回我的浏览器。(参见代码打击)
如何排除这些特定方法,是否有@NonResponseBody
注释?
这是配置部分。
<mvc:annotation-driven content-negotiation-manager="cnManager" />
<bean id="cnManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="useJaf" value="false" />
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp" />
<property name="prefix" value="/pages/" />
</bean>
这是我的控制器。
@RestController
public class MsgController {
@Autowired
private MsgService msgService;
/**
* RESTful method, list all msgs.
*
* @return
*/
@RequestMapping(value = "/admin/msg", produces = { "application/json" }, method = GET)
public Result list() {
try {
List<Msg> msgs = msgService.findAll();
return Result.ok(msgs);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return Result.error(e.getMessage());
}
}
/**
* View-based method, list all msgs.
*
* @return
*/
@RequestMapping(value = "/admin/msg", method = GET)
public String list(Model model) {
// Call RESTful method to avoid repeating lookup logic
model.addAttribute("msgs", list().getData());
// Return the view to use for rendering the response
return "admin/msg/msg_index";
}
答案 0 :(得分:1)
没有。将您的方法移至未使用@RestController
或@ResponseBody
注释的其他控制器。