Spring Boot Java后端编码的硬代码通用?
这是我在后端工作的第一天。我的背景是前端Java开发人员。我已经看过很多教程,我注意到在所有教程中,字符串文字都是硬编码的,没有类/方法/内联文档。这对于后端编码是否正常/可接受
示例教程
https://www.jackrutorial.com/2018/04/spring-boot-user-registration-login.html
https://medium.com/@kamer.dev/spring-boot-user-registration-and-login-43a33ea19745
在基于Java的前端项目中,我永远不会硬编码任何值。以此为例,
package com.jackrutorial.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.jackrutorial.model.User;
import com.jackrutorial.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value= {"/", "/login"}, method=RequestMethod.GET)
public ModelAndView login() {
ModelAndView model = new ModelAndView();
model.setViewName("user/login");
return model;
}
@RequestMapping(value= {"/signup"}, method=RequestMethod.GET)
public ModelAndView signup() {
ModelAndView model = new ModelAndView();
User user = new User();
model.addObject("user", user);
model.setViewName("user/signup");
return model;
}
@RequestMapping(value= {"/signup"}, method=RequestMethod.POST)
public ModelAndView createUser(@Valid User user, BindingResult bindingResult) {
ModelAndView model = new ModelAndView();
User userExists = userService.findUserByEmail(user.getEmail());
if(userExists != null) {
bindingResult.rejectValue("email", "error.user", "This email already exists!");
}
if(bindingResult.hasErrors()) {
model.setViewName("user/signup");
} else {
userService.saveUser(user);
model.addObject("msg", "User has been registered successfully!");
model.addObject("user", new User());
model.setViewName("user/signup");
}
return model;
}
@RequestMapping(value= {"/home/home"}, method=RequestMethod.GET)
public ModelAndView home() {
ModelAndView model = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
model.addObject("userName", user.getFirstname() + " " + user.getLastname());
model.setViewName("home/home");
return model;
}
@RequestMapping(value= {"/access_denied"}, method=RequestMethod.GET)
public ModelAndView accessDenied() {
ModelAndView model = new ModelAndView();
model.setViewName("errors/access_denied");
return model;
}
}
字符串值,如“用户/注册”或“错误/访问被拒绝”,ect可多次使用。最好创建常量以避免任何拼写错误,并有一个参考点,等等。最终,我只是想了解什么是后端开发的最佳实践。如果这是前端代码,我的线索会给我一个新的
# 1 楼答案
正如
@drodil
所指出的关于可维护性的问题,我个人不做常量,因为此时此地更容易看到端点。您可以添加Controller
级别@RequestMapping
并将端点的公共部分移动到那里除此之外,还可以进行控制器测试,这样就不会出现拼写错误的问题
这两种方法都可以很好地工作
# 2 楼答案
这基本上只是可维护性的问题。毕竟,编译过程中的优化很可能能够处理这些文本,因此它们只创建一次,因此不会影响性能或内存使用
对我来说,对不变的字符串使用常量一直都是不需要动脑筋的,尤其是当它们在代码中被多次使用时
如果我是你的主角,看到这一幕,我可能也会给你撕一个新的;)