Tag: spring mvc

Spring MVC请求映射明确禁止参数

我遇到了Spring(3.1)映射难题:我有一个简单的findAll方法: @RequestMapping( method = RequestMethod.GET ) @ResponseBody public List findAll(){ return findAllInternal(); } 这映射在以下URI(这是好的): /user 。 但它也映射到: /user?bla=8 ,这不好。 有没有办法明确指出这个特定的映射不包含参数的事实? 我试图在@RequestMapping指定params ,但我看不出这样做的明确方法。 任何帮助表示赞赏。 谢谢。 尤金。

如何在Spring中为html创建视图解析器?

我决定创建一个没有JSP的web-app,但只使用位于WEB-INF / pages目录下的HTML页面,我遇到了这个问题。 我已经制作了视图解析器: 我还导入了WEB-INF / pages中的所有资源: 我的控制器有以下视图: @PreAuthorize(“isAuthenticated()”) @RequestMapping(“/”) public String indexPage() { return “redirect:/index.html”; } 它适用于映射“/”(如果未经过身份validation,则重定向到登录页面),但由于将此页面作为静态资源导入,因此对于URL“/index.html”不安全(但如果不是,它将无法正常工作)导入它)。

在Spring MVC中,从哪里开始和结束计数器来测试速度执行时间?

我想知道一个特定页面渲染需要多长时间,Spring MVC框架中的各个部分可以启动和停止我的计时器? 顺便说一下,我应该使用什么java类来获得最准确的时间(以毫秒为单位)?

通过Spring MVC将二进制文件上传到MySQL的正确方法

我正在尝试将文件上传到MySQL,但是我无法正确地执行此操作。 我正在使用云平台将Java Spring应用程序作为网站运行。 控制器: byte[] bytes = file.getBytes(); // file is MultipartFile DFile newFile = new DFile(); // my object newFile.setName(name); // name column newFile.setType(type); // Blob blob = new SerialBlob(bytes); // set the blob from binary newFile.setData(blob); fileService.insertFile(newFile); // calls to insert into MySQL 道:两种不同的方式 length = (int)blob.length(); byte[] b = blob.getBytes(1, length); InputStream […]

限制某些数据访问的正确方法是什么

我正在开发一个应用程序,每个员工都有自己的客户。 当员工想要显示,修改或删除客户时,我想确保该客户是该员工之一。 这是因为做这些动作的url就像 www.xxx.com/customers/update/{idCustomer} 我现在有效访问客户的方式是通过服务调用(具有数据库访问权限)来确保该客户是该员工之一。 此应用程序是使用Spring Security在Spring MVC中编写的。 我想知道是否有更好的方法来进行相同的限制访问?

Spring MVC缺少矩阵变量

我正在尝试使用SpringMVC(来自Spring boot 1.2.3.RELEASE)向我的Rest Controller添加矩阵参数(或矩阵变量)这是我的代码: @RestController public class SubAgentsController { @RequestMapping(value = “/{subagents}”, method = RequestMethod.GET) public SubAgent subAgents(@MatrixVariable(value=”agentName”, pathVar=”subagents”) String agentName) { System.out.println(agentName); } } 不幸的是,当我尝试获取: http:// localhost:8080 / subagents; agentName = hello 这就是我收到的答案: 出现意外错误(type = Bad Request,status = 400)。 缺少类型为String的方法参数的矩阵变量’agentName’ 我做错了什么 ? 根据应该工作的http://docs.spring.io/spring-framework/docs/3.2.0.M2/reference/html/mvc.html 🙁 谢谢你的回答!

如何将@RequestBody与JSONP请求一起使用?

我正在尝试使用jsonp数据类型执行ajax请求,因为群集环境中存在跨域问题。 我可以对没有@RequestBody参数映射的方法发出jsonp请求,但是当我尝试使用@RequestBody参数实现RequestMapping时,我得到415 Unsupported Media Type错误。 通常当我遇到这个问题时,由于某些属性在发布的json对象和它在Spring中映射到的Java对象之间没有正确映射。 但我能找到的唯一不同之处是使用jsonp它会添加一个名为callback的parm和一个名为下划线“_”的parm。 所以我将标记@JsonIgnoreProperties(ignoreUnknown = true)添加到我的Java对象中,并认为应该解决这个问题,但它仍然会抛出此错误。 还有什么我需要做的吗? 编辑:我现在在Spring的调试日志输出中看到此堆栈跟踪:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’application / octet-stream’ $.ajax({ url : ‘http://blah/blah.html’, data : { abc : ‘123’ }, (I also tried to JSON.stringify the object but no difference) dataType : ‘jsonp’, success : function(response) { alert(‘ok ‘+JSON.stringify(response)); }, fail : function(response) { alert(‘error’+JSON.stringify(response)); } }); Spring控制器是: @RequestMapping({ “blah/blah” […]

为什么ThreadLocal实用程序总是在Spring MVC应用程序中返回null?

我编写了这个实用程序类来保存Spring MVC应用程序中的临时数据: public abstract class FooUtil { private static final ThreadLocal threadFoo = new ThreadLocal(); public static String getFooId(){ return threadFoo.get(); } public static void setFooId(String fooId){ threadFoo.set(fooId); } public static void removeFooId(){ threadFoo.remove(); } } 所以我调用FooUtil.setFooId(“foo”) 。 但是当我稍后调用FooUtil.getFooId() ,它总是返回null 。 我需要一个构造函数吗? 或者这不应该是一个抽象类? 我不知道。

为可信空间定制Spring Security

服务在可信空间中的网关之后工作(gateWayvalidationOAuth令牌并仅向服务提供唯一的用户ID,其他情况下它重定向以validation服务)。 我想在服务中使用spring security来validationuserId的权限。 所以我添加了CustomUserDetailsService @Service( “的UserDetailsS​​ervice”) 公共类CustomUserDetailsS​​ervice实现UserDetailsS​​ervice { @Autowired(required = false) private ContextSsoActiveProfileIdProvider contextSsoActiveProfileIdProvider; @Autowired private GrantedAuthorityService grantAuthorityService; @覆盖 public User loadUserByUsername(final String username)throws UsernameNotFoundException { //使用身份validation服务validation它,但没有令牌,仅限userId,因此信任网关服务。 返回新用户( 将String.valueOf(contextSsoActiveProfileIdProvider.getSsoActiveProfileId()), “authenticatedWithGateWay” grantedAuthorityService.getGrantedAuthoritiesForCurrentUser() ); } } 其中contextSsoActiveProfileIdProvider.getSsoActiveProfileId()返回uniqueUserId和grantedAuthorityService.getGrantedAuthoritiesForCurrentUser()返回权限。 该服务在受信任区域中启动,因此我已通过下一步方式配置安全性: @EnableWebSecurity @组态 公共类SecurityConfiguration扩展了WebSecurityConfigurerAdapter { @Autowired private UserDetailsS​​ervice userDetailsS​​ervice; @覆盖 protected void configure(HttpSecurity http)抛出Exception { HTTP .authorizeRequests() .antMatchers( “/ **”)permitAll(); […]

Spring Web Application已初始化两次

我发现我的spring web项目在tomcat上初始化了两次,这里是打印的消息 第一次: INFO: Initializing Spring root WebApplicationContext INFO 2015-01-08 15:18:04 ContextLoader Root WebApplicationContext: initialization started INFO 2015-01-08 15:18:04 XmlWebApplicationContext Refreshing Root WebApplicationContext: startup date [Thu Jan 08 15:18:04 GMT+08:00 2015]; root of context hierarchy INFO 2015-01-08 15:18:04 XmlBeanDefinitionReader Loading XML bean definitions from file [/home/pinkdahlia/develop/apache-tomcat-7.0.55/webapps/vips-mobile-mlisting-backend/WEB-INF/classes/spring-shiro.xml] INFO 2015-01-08 15:18:04 XmlBeanDefinition Reader Loading XML bean definitions […]