Tag: spring mvc

您在Spring MVC应用程序中使用什么命名约定?

我坚持在弹簧应用程序中弄清楚服务层的一个好的命名约定。 对于服务层中的每个类,我首先编写它应该实现的接口,然后编写实际的类。 所以例如我有以下接口: public interface UserAccountManager{ public void registerUser(UserAccount newUserAccount); public void resetPassword(UserAccount userAccount); … } 然后是实现类… 在这里有什么错误是UserAccountManager是一个实现类的好名字,所以我不得不给它一个愚蠢的名字,如SimpleUserAccountManager或UserAccountDbManager。 到目前为止,您使用的一些惯例是什么? 把实现类放在一个不同的包中并给它们起一个和接口一样的名字是个好主意? 你还有什么想在以Service结尾的名字上使用以Manager结尾的名字?

在JSP ELexpression式中获得Spring Security Principal

我正在使用Spring MVC和Spring安全版本3.0.6.RELEASE。 在我的JSP中获取用户名最简单的方法是什么? 甚至只是用户是否login? 我可以想到一些方法: 1.使用scriptlet 使用这样的脚本来确定用户是否login: <%=org.springframework.security.core.context.SecurityContextHolder.getContext() .getAuthentication().getPrincipal().equals("anonymousUser") ? "false":"true"%> 我不喜欢使用scriptlet,而且我想在一些<c:if>标签中使用它,这需要把它作为页面属性。 2.使用SecurityContextHolder 我可以再次从我的@Controller使用SecurityContextHolder并将其放在模型上。 不过,我在每一页上都需要这个,所以我宁愿不必在每一个控制器中添加这个逻辑。 我怀疑有一个更清洁的方法来做到这一点…

IncompatibleClassChangeError:class ClassMetadataReadingVisitor将接口ClassVisitor作为超类

我使用spring-mvc和mongodb作为数据库构build了一个Web应用程序。 我用maven3来构build应用程序。 项目build立成功,但是当应用程序启动时,我收到日志中的以下错误,因为我的应用程序无法启动。 这曾经在几个月前工作。 引起:java.lang.IncompatibleClassChangeError:类org.springframework.core.type.classreading.ClassMetadataReadingVisitor具有接口org.springframework.asm.ClassVisitor作为超类 请让我知道,如果任何指针或如果你们需要更多的信息。

Spring REST多个@RequestBody参数,可能吗?

我已经实现了一个Spring RESTful Web服务。 使用Jackson JSON进行对象映射。 我有一个接受两个参数的方法。 public Person createPerson( @RequestBody UserContext userContext, @RequestBody Person person) 客户端如何在多个JSON对象在主体中传递的地方构造一个请求? 这可能吗? – 斯里兰卡

为什么我们不应该创build一个Spring MVC控制器@Transactional?

关于这个话题已经有几个问题了,但是没有任何回应真的提供了论据来解释为什么我们不应该使Spring MVC控制器成为Transactional 。 看到: 事务不能正常工作 – Spring / MyBatis 对于Web MVC Spring应用程序应该@Transactional去控制器或服务? 制作Spring 3 MVC控制器方法Transactional Spring MVC控制器事务 所以为什么? 有不可克服的技术问题吗? 有build筑问题吗? 是否有性能/死锁/并发问题? 有时需要多个独立的交易? 如果是,那么用例是什么? (我喜欢简化的devise,调用服务器要么完全成功,要么完全失败,这听起来是一个非常稳定的行为) 背景:几年前,我在一个团队中工作了一个在C#/ NHibernate / Spring.Net中实现的相当庞大的ERP软件。 服务器的往返是这样实现的:事务在进入任何控制器逻辑之前被打开,并在退出控制器之后被提交或回滚。 交易是在框架内pipe理的,所以没有人需要关心它。 这是一个绝妙的解决scheme:稳定,简单,只有less数架构师不得不关心交易问题,其余的团队只是实现了function。 从我的angular度来看,这是我所见过的最好的devise。 当我尝试用Spring MVC重现相同的devise时,我进入了懒惰加载和事务问题的噩梦,每次都有相同的答案:不要使控制器事务,但为什么? 预先感谢您的build议答案!

Spring数据jpa-没有定义名为“entityManagerFactory”的bean; 注入自动装载的依赖关系失败

我正在开发应用程序使用spring的数据jpa,hibernate,mysql,tomcat7,maven和它的创build错误。我试图弄清楚,但我失败了。 错误是在设置构造函数参数的时候无法parsingbean的'entityManagerFactory'引用; 没有定义名为“entityManagerFactory”的bean; 注入自动装载的依赖关系失败 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initDbService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.wahid.cse.repository.RoleRepository org.wahid.cse.service.InitDbService.roleRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleRepository': Cannot create inner bean '(inner bean)#c08f81' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: […]

在Spring Boot的application.properties中使用envvariables

我们正在开发一个Spring Boot web应用程序,我们使用的数据库是MySql ; 我们的设置是我们首先在本地进行testing(意味着我们需要在我们的PC上安装MySql); 然后我们推到Bitbucket ; Jenkins自动检测到对Bitbucket的新推送,并对其进行构build(对于Jenkins mvn构build,我们还需要在运行Jenkins的虚拟机上安装MySql)。 如果Jenkins构build通行证,我们将代码推送到OpenShift上的应用程序(使用Jenkins上的Openshift部署插件)。 我们所遇到的问题你可能已经知道了: 在application.properties我们不能硬编码MySql的信息。 由于我们的项目将在3个不同的地方( 本地 , Jenkins和OpenShift )运行,因此我们需要在application.properties中使数据源字段dynamic化(我们知道有不同的方法,但我们现在正在开发此解决scheme) 。 spring.datasource.url = spring.datasource.username = spring.datasource.password = 我们提出的解决scheme是我们在本地创build系统环境variables ,并在Jenkins虚拟机(以OpenShift命名的方式命名它们)并分配给它们正确的值: export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost" export OPENSHIFT_MYSQL_DB_PORT="3306" export OPENSHIFT_MYSQL_DB_USERNAME="root" export OPENSHIFT_MYSQL_DB_PASSWORD="123asd" 我们已经完成了这个工作。 我们还检查了Map<String, String> env = System.getenv(); 环境variables可以变成javavariables,如下所示: String password = env.get("OPENSHIFT_MYSQL_DB_PASSWORD"); String userName = env.get("OPENSHIFT_MYSQL_DB_USERNAME"); String sqlURL = env.get("OPENSHIFT_MYSQL_DB_HOST"); String sqlPort […]

从Spring MVC发送JSON时dynamic忽略Java对象的字段

我有像这样的模型类,hibernate @Entity @Table(name = "user", catalog = "userdb") @JsonIgnoreProperties(ignoreUnknown = true) public class User implements java.io.Serializable { private Integer userId; private String userName; private String emailId; private String encryptedPwd; private String createdBy; private String updatedBy; @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "UserId", unique = true, nullable = false) public Integer getUserId() { return this.userId; } […]

设置多个@ControllerAdvice @ExceptionHandlers的优先级

我有@ControllerAdvice批注了多个类,每个都有一个@ExceptionHandler方法。 一个处理Exception的意图是,如果没有find更具体的处理程序,应该使用这个。 可悲的是,Spring MVC似乎总是使用最通用的情况( Exception ),而不是更具体的情况(例如IOException )。 这是如何期望Spring MVC的行为? 我试图从泽西模拟一个模式,它评估每个ExceptionMapper (等价的组件),以确定它处理的声明types距离抛出的exception有多远,并始终使用最近的祖先。

Spring MVC – 为什么不能一起使用@RequestBody和@RequestParam

使用具有Post请求和Content-Type应用程序的HTTP dev客户端/ x-www-form-urlencoded 1)只有@RequestBody Request – localhost:8080 / SpringMVC / welcome在Body – name = abc中 码- @RequestMapping(method = RequestMethod.POST) public String printWelcome(@RequestBody String body, Model model) { model.addAttribute("message", body); return "hello"; } //如预期的那样给body赋予“name = abc” 2)只有@RequestParam Request – localhost:8080 / SpringMVC / welcome在Body – name = abc中 码- @RequestMapping(method = RequestMethod.POST) public String printWelcome(@RequestParam String […]