小编Vit*_*nov的帖子

Spring MVC:如何修改从控制器发送的json响应

我用这样的控制器构建了一个json REST服务:

@Controller
@RequestMapping(value = "/scripts")
public class ScriptController {

    @Autowired
    private ScriptService scriptService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public List<Script> get() {
        return scriptService.getScripts();
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,但现在我需要修改所有响应并向所有响应添加"status"和"message"字段.我读过一些解决方案:

  1. 从一些特定类的所有控制器方法对象返回,例如,RestResponse,它将包含"status"和"message"字段(但它不是一般的解决方案,因为我将不得不修改我的所有控制器并以新的方式编写新的控制器)
  2. 拦截所有控制器方法与方面(但在这种情况下我不能改变返回类型)

如果我想将从控制器方法返回的值包装到类的对象中,您能否提出一些其他的,一般的和正确的解决方案:

public class RestResponse {

    private int status;
    private String message;
    private Object data;

    public RestResponse(int status, String message, Object data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    //getters and setters
}
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc

21
推荐指数
2
解决办法
3万
查看次数

使用springSecurityFilterChain从Spring MockMvc清空响应头

我为Spring MVC应用程序编写单元测试.

这是我的mockMvc初始化代码:

@Configuration
public class SpringMockFactory {
    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private FilterChainProxy springSecurityFilterChain;

    @Bean
    @Scope("prototype")
    public MockMvc getMockMvc() {
        return webAppContextSetup(wac)
                .addFilter(springSecurityFilterChain)
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,但是当我添加springSecurityFilterChain时,响应头总是空的(这很重要,因为我想检查Set-Cookie头).

我使用此代码执行操作:

resultActions = mockMvc.perform(post("/api/login")
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .param("username", "sysadmin")
                .param("password", "password")
).andDo(print());
Run Code Online (Sandbox Code Playgroud)

这是andDo(print())的结果:

MockHttpServletResponse:
          Status = 200
   Error message = null
         Headers = {}
    Content type = null
            Body = {"status":200}
   Forwarded URL = null
  Redirected URL = null
         Cookies = []
Run Code Online (Sandbox Code Playgroud)

问题是当我将springSecurityFilterChain与mockMvc一起使用时,为什么响应标头为空?以及如何阅读它们?

spring spring-mvc spring-security spring-test

5
推荐指数
1
解决办法
1310
查看次数

IntelliJ IDEA无法解析弹簧导入的文件

我有一个带有父pom文件和两个模块(名为"app"和"modules")的multimodule maven项目.在"app"的测试资源中,我有一个spring配置,其中包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="file:src/main/webapp/WEB-INF/spring-config/security.xml"/>
    <import resource="file:src/main/webapp/WEB-INF/spring-config/mvc-servlet.xml"/>
    <import resource="file:src/main/webapp/WEB-INF/spring-config/common.xml"/>

    <import resource="db-test.xml"/>
    <import resource="utilsContext.xml"/>
</beans>
Run Code Online (Sandbox Code Playgroud)

IntelliJ IDEA 13和14找不到在我的情况下作为资源导入的文件(security.xml,mvc-servlet.xml和common.xml),因此IDEA无法解析弹簧上下文中对象的注入.如果我将路径更改为文件:app/src/main/webapp/WEB-INF/spring-config/security.xml,那么一切正常,但它会崩溃maven测试.

请告诉我如何配置IntelliJ IDEAs弹簧文件的分辨率.

java spring intellij-idea maven

4
推荐指数
1
解决办法
5586
查看次数

FirebaseUI iOS删除取消按钮

我是iOS开发的新手.我想用Firebase建立授权.我正在使用FirebaseUI-iOS.我想隐藏初始屏幕上的取消按钮.你有什么想法怎么做吗?

我以编程方式创建Firebase authViewController:

import UIKit
import Firebase
import FirebaseAuthUI
import FirebaseGoogleAuthUI

class AuthViewController: UIViewController {

fileprivate var _authHandle: FIRAuthStateDidChangeListenerHandle!
var user: FIRUser?

override func viewDidLoad() {
    super.viewDidLoad()
    configureAuth()
}

func configureAuth() {
    let provider: [FUIAuthProvider] = [FUIGoogleAuth()]
    FUIAuth.defaultAuthUI()?.providers = provider
    FUIAuth.defaultAuthUI()?.isSignInWithEmailHidden = true

    // listen for changes in the authorization state
    _authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in
        // check if there is a current user
        if let activeUser = user {
            // check if the current app …
Run Code Online (Sandbox Code Playgroud)

ios firebase swift firebase-authentication firebaseui

3
推荐指数
2
解决办法
1220
查看次数

如何获得纵横比为 1:1 和最大可用分辨率的网络摄像头流

基本上问题就在标题中。这是关于 WebRTC 和 getUserMedia 功能。类似的问题在这里:如何在 WebRTC 中始终保持 1:1 宽高比视频。但就我而言,我需要使用 MediaRecorder 录制流,仅使用 css 裁剪视频元素是不够的。我对 getUserMedia 约束有点困惑。有 aspectRatio 参数,但我没有管理如何用它实现所需的结果。对我有用的是以这种方式定义约束:

const constraints = {
  audio: true,
  video: {
    width: { exact: 720 },
  }
};
Run Code Online (Sandbox Code Playgroud)

但它不会自动定义最大分辨率。你有什么想法如何以聪明的方式做到这一点吗?

javascript webrtc getusermedia

2
推荐指数
1
解决办法
1712
查看次数

如何将内部版本号传递给 maven-cucumber-reporting 插件

我使用此配置运行 maven-cucumber-reporting 插件,结果报告页面上的内部版本号始终等于 1。

        <plugin>
            <groupId>net.masterthought</groupId>
            <artifactId>maven-cucumber-reporting</artifactId>
            <version>0.0.6</version>
            <executions>
                <execution>
                    <id>execution</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                         <projectName>${project.name}</projectName>
                         <outputDirectory>${project.build.directory}/cucumber-html-reports</outputDirectory>
                            <cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
                        <enableFlashCharts>false</enableFlashCharts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

如何传递内部版本号,例如从 Maven?

cucumber maven cucumber-jvm

1
推荐指数
1
解决办法
1922
查看次数