我有两个Controller类:
@Controller
@RequestMapping( "/controller" )
public class Controller_Indirect{
@RequestMapping( "/setContextIndirect" )
public @ResponseBody ModelAndView setContextIndirect( HttpServletRequest request, @RequestParam Message message )
throws Exception{
BigMessage bigMessage = new BigMessage();
bigMessage.setMessage( message );
bigMessage.setMessageId( "Sample Message ID" );
return new ModelAndView( "forward:/setContext", "BigMessage", bigMessage );
}
}
@Controller
@RequestMapping( "/controller2" )
public class Controller_Direct{
@RequestMapping( "/setContext" )
public @ResponseBody JSONOutput setContextDirect( HttpServletRequest request, @RequestBody BigMessage bigMessage )
throws Exception{
JSONOutput jsonOutput = new JSONOutput();
OutgoingMessage outgoingMessage = new OutgoingMessage();
..
// Some code
..
jsonOutput.setData( outgoingMessage );
jsonOutput.setSuccess( true );
return jsonOutput;
}
}
这里的JSONOutput有两个字段数据和isSuccess。 data是方法调用返回的对象--BigMessage 因此,有可能调用这些Controller方法中的任何一个。 direct方法返回一个JSONOutput对象,而indirect方法返回一个ModelAndView对象。
有什么方法可以做到这一点吗? (获取与Direct方法的响应相同的JSONOutput,然后我可以从Indirect方法返回):
@Controller
@RequestMapping( "/controller" )
public class Controller_Indirect{
@RequestMapping( "/setContextIndirect" )
public @ResponseBody JSONOutput setContextIndirect( HttpServletRequest request, @RequestBody Message message ){
BigMessage bigMessage = new BigMessage();
bigMessage.setMessage( message );
bigMessage.setMessageId( "Sample Message ID" );
JSONOutput jsonOutput = new JSONOutput();
jsonOutput = (JSONOutput) new ModelAndView( "forward:/setContext", "BigMessage", bigMessage );
// This JSONOutput should have the OutgoingMessage set as data field.
return jsonOutput;
}
}
@Controller
@RequestMapping( "/controller2" )
public class Controller_Direct{
@RequestMapping( "/setContext" )
public @ResponseBody JSONOutput setContextDirect( HttpServletRequest request, @RequestBody BigMessage bigMessage ){
JSONOutput jsonOutput = new JSONOutput();
OutgoingMessage outgoingMessage = new OutgoingMessage();
..
// Some code
..
jsonOutput.setData( outgoingMessage );
jsonOutput.setSuccess( true );
return jsonOutput;
}
}
目前,如果我执行此操作BigMessage bigMessage = (BigMessage) modelAndView.getModel().get( "BigMessage" );
,我会将BigMessage
作为模型对象。
我需要JSONOutput
,其中包含OutgoingMessage
。
任何想法都会很棒。提前谢谢。
答案 0 :(得分:0)
您希望从同一个控制器获得modelAndView
和json
输出,然后您可以移至contentNegotiationViewResolver
以获得结果。