如何让spring mvc controller的同一个方法,根据逻辑判断返回json或者html视图
1个回答
展开全部
package com.defonds.oauth.user.mvc;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.defonds.oauth.GlobalConstant;
import com.defonds.oauth.common.exception.UserOAuthException;
import com.defonds.oauth.common.util.JsonView;
import com.defonds.oauth.user.response.UserResponse;
import com.defonds.oauth.user.service.UserService;
@Controller
public class UserController {
private static final Log log = LogFactory.getLog(UserController.class);
private UserService userService;
/**
* 用户注册接口(浏览器终端、手机终端共同走此接口)
* 判断依据:浏览器接口传递过来一 hidden 变量 browser,其值为 true;否则判定为其他终端
* @param username 用户名【必须】
* @param password 密码 【必须】
* @param email 邮箱地址【必须】
* @param browser 浏览器【可选】,值为 true 者为浏览器
* @return ModelAndView:浏览器终端返回 jsp 页面;其他终端返回 JSON 串
* 备注:Spring 注解绑定时 @RequestParam("email") String email,@RequestParam 的 required 参数默认为 true
* 如果手机终端用户 HTTP 请求没有 email 参数,将产生异常
* 所以将 email 等传入参数是否包含的验证交由服务器处理,为空返回相应异常的 JSON 串
*/
@RequestMapping("/user/register")
public ModelAndView doRegister(HttpServletResponse response,
@RequestParam(value = "username", required = false) String username,
@RequestParam(value = "password", required = false) String password,
@RequestParam(value = "email", required = false) String email,
@RequestParam(value = "browser", required = false) String browser) {
if (browser != null && browser.equals("true")) {//有值且为 true,是浏览器终端发起的请求
try {
userService.addOauthUser(username, password, email);//处理注册相关业务逻辑
return new ModelAndView("user/registerSuccess");//返回到注册成功页面
} catch (UserOAuthException uoae) {//自定义业务逻辑异常
log.debug(uoae.getMessage());
ModelAndView mav = new ModelAndView("user/registerFailed");
mav.addObject("userOAuthException", uoae);
return mav;//返回注册失败页面
} catch (Exception e) {//系统异常
log.error(e.getMessage(), e);
UserOAuthException userOAuthException = new UserOAuthException(
UserOAuthException.ERROR_11001,
UserOAuthException.ERROR_CODE_11001,
UserOAuthException.ERROR_DESC_11001,
e.getMessage());
ModelAndView mav = new ModelAndView("user/registerFailed");
mav.addObject("userOAuthException", userOAuthException);
return mav;//返回注册失败页面
}
} else {//无值或不为 true,是浏览器之外的终端发起的请求
try {
userService.addOauthUser(username, password, email);//处理注册相关业务逻辑
return JsonView.Render(new UserResponse(
GlobalConstant.SUCCESS_CODE_11000,
username,
email), response);//其他终端,返回注册成功的 JSON 串
} catch (UserOAuthException uoae) {//自定义业务逻辑异常
log.debug(uoae.getMessage());
return JsonView.Render(uoae, response);//其他终端,返回注册失败的 JSON 串
} catch (Exception e) {//系统异常
log.error(e.getMessage(), e);
return JsonView.Render(new UserOAuthException(
UserOAuthException.ERROR_11001,
UserOAuthException.ERROR_CODE_11001,
UserOAuthException.ERROR_DESC_11001,
e.getMessage()), response);
}
}
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询