Tag: spring mvc

尽量减少控制器责任

理想情况下,Spring MVC应用程序中的控制器必须接收请求,将请求发送到API,将(调用的)结果加载到模型上(对于视图随后呈现它)并转发到视图。 他们不应该再做了。 我的控制器今天所做的远不止这些,我想将某些重要性从控制器转移到其他API。 我今天的应用程序设计(非常典型): controller Service API DAO DB 今天的控制器填补了Web应用程序需要的内容和Service API提供的内容之间的差异。 我想在控制器和服务API之间放置额外的层/层,以便在这个delta处咀嚼。 我的问题是这些层应该是什么层以及这些新层的责任应该是什么? 我目前的想法如下 controller controller helper Business API Service API DAO DB 控制器助手(Web上下文感知 – 将依赖于Model,HttpServlet和其他Web上下文类): 将实体转换为DTO对象(2路) 将ID解析为实体。 例如,Controller查找学生ID(使用密钥)并将其转换为Student实体。 Business API(没有Web上下文依赖 – 可以通过JUnit测试): 充当门面。 调用多个服务API以实现一个业务请求。 提供专门用于Web应用程序的API。 你会以不同的方式解决这个问题吗? 是否有与此特定问题相关的资源(书籍,文章等)? 之前的一些讨论没有回答我的问题: 设计mvc控制器层 服务层=应用层= GRASP控制器层 移动validation,Html助手到MVC中的服务层 谢谢,维杰

使用Java中的Spring Properties和CrossOrigin Annotation或Spring-Config XML

我在Spring中使用CrossOrigin注释。 现在我想将一个Property作为值注入注释。 我不打算这个工作。 现在我像这样访问我的财产: @Value(“${settings.cors_origin}”) String cors_origin; 我想将此属性注入CrossOrigin注释: …. @CrossOrigin(origins = cors_origin) @RequestMapping(value=”/request”, method= RequestMethod.GET, produces = “application/json”) public ResponseEntity getMethod() { … 我尝试过这样的事: @CrossOrigin(origins = “${settings.cors_origin}”) 编辑1: 现在我尝试在我的spring配置中全局设置CORS-Header: 此设置也无效! 允许的原点与属性文件中指定的原点不同。 我认为它不会将变量转换为值? 当我在spring config allowed-origin中手动设置IP地址时,它正在工作。 设置有问题…… appsettings.properties: test=http://192.168.1.200 编辑2: 解决了 经过一段时间的故障排除后,我现在为我解决了这个问题:-)现在我再次使用注释@CrossOrigin。 我不得不将RequestMethod选项添加到Spring RequestMapping: @RestController @CrossOrigin(origins = {“${settings.cors_origin}”}) @RequestMapping(“/api/v1″) public class MainController { @RequestMapping(value=”/request”, method = […]

Spring Security – 405请求方法’POST’不受支持

我已经为我的项目实现了Spring Security,但是当我尝试登录时,我获得状态csrf我已经在form添加了csrf令牌。 这是我发送用户名和密码时出现的错误: HTTP Status 405 – Request method ‘POST’ not supported 春季版:4.0.2.RELEASED Invalid username and password. You have been logged out successfully. 安全配置: @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Autowired @Qualifier(“G2BUserDetailsService”) UserDetailsService userDetailsService; @Autowired public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() […]

静态资源的URI中的上下文路径,我真的需要指定它吗?

我有一个简单的网络应用程序 webapp static images – a.gif pages – test.html WEB-INF pages – test.jsp 在test.html中,有 问题是,在我将uri更改为之前,图像不会显示 但是我在URI上加载test.html http://server/web app name/static/pages/test.html 我在我的web.xml中配置了静态资源映射,如下所示。 springWeb org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext-web.xml 1 springWeb / resourceServlet org.springframework.js.resource.ResourceServlet 1 resourceServlet /static/* 我错过了什么吗? 我确实希望在DEV阶段将这些静态资源保留在应用程序中,而不是将它们移动到HTTP服务器。 非常感谢。

如何让Spring Controller从POJO返回CSV?

给出一个简单的Java对象: public class Pojo { private String x; private String y; private String z; //… getters/setters … } 是否有一些我可以放在我的项目上的lib,它将构成一个像这样的控制器: @RequestMapping(value=”/csv”, method=RequestMethod.GET, produces= MediaType.TEXT_PLAIN) @ResponseBody public List csv() { //Some code to get a list of Pojo objects //… return myListOfPojos; } 要生成我的Pojos的csv文件? 对于Json结果,我使用Jackson lib。 我需要另一个用于CSV结果的lib。

为什么我的JSP视图没有得到解决?

我的dispatcher-servlet.xml 这是我的控制者 package com.example.spring; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping(“/form.htm”) public ModelAndView hello(Model model) { return new ModelAndView(“index”); } } 当我在调试中使用应用程序时,我在控制台中看到了这一点。 WARNING: No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name ‘dispatcher’ 控制器被命中,并返回视图,但无法解析。

Spring数据rest – Json中的禁用自我链接(HAL)

我是spring-data-rest的新手。 在我的应用程序中,当我rest时,我正在获取json,其中包括自我链接和外键表的链接。 但我想要json而不是链接。 我使用telosys工具生成器生成了我的代码。 这是我的JSON,当我对数据库中的“商品”表进行REST调用时,我得到了什么: { “id” : 1, “rate”: 300, “type”: “item”, “shortDescription”: “test”, “longDescription”: “test test”, “_links”: { “self”: { “href”: “http://localhost:8080/sportsrest/merchandises/1” }, “listOfMerchandiseAttribute”: { “href”: “http://localhost:8080/sportsrest/merchandises/1/listOfMerchandiseAttribute” }, } } 但是,我想获得listOfMerchandiseAttribute的JSON,而不是获取“listOfMerchandiseAttribute”的链接。 listOfMerchandiseAttribute是我在数据库中的另一个表 那就是我希望我的json像这样: { “id”: 1, “rate”: 300, “type”: “item”, “shortDescription”: “test”, “longDescription”: “test test”, “_links”: { “self”: { “href”: “http://localhost:8080/sportsrest/merchandises/1” }, “listOfMerchandiseAttribute”: […]

Spring REST中的SSE实现

任何人都可以使用Spring Rest提供SSE(服务器发送事件)的示例吗? 基本上我有一个请求,它的响应将由服务器以多个块发送。 我想在Spring REST Api中使用服务器和客户端实现,而不需要像泽西这样的第三方restapi。

自动化应用程序上下文

我试图在我的Spring MVC项目ImageCreatorUtil WebApplicationContext自动assembly到我创建的类ImageCreatorUtil中。 在使用应用程序上下文的类中执行方法时,我总是收到一个NPE。 请务必注意,此方法由另一个配置文件中定义的ApplicationListener调用。 我不确定为什么自动assembly不起作用。 有人可以提供任何建议吗? servlet的context.xml中 ImageCreatorUtil.java @Component public class ImageCreatorUtil { static Logger logger = LoggerFactory.getLogger(ImageCreatorUtil.class); static final String IMAGE_PATH_FRAGMENT = “/resources/images/resume/skills/uploaded-icons/”; @Autowired private WebApplicationContext context; /** * Creates the provided file in the resources directory for access by the * web application. * * @param appContext * @param image */ public […]

如何使用Spring 4在我的webSocket服务器中捕获连接事件?

我按照https://github.com/rstoyanchev/spring-websocket-portfolio和这个http://assets.spring.io/wp/WebSocketBlogPost进行了与spring 4,STOMP和sock.js的简单web套接字通信。 HTML 好吧,我想知道是否有可能捕获连接事件,例如新客户端连接到我的服务器或客户端断开连接时,是否可以在Spring 4.0.0中进行?