且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将JSON对象作为PathVariable传递给spring控制器

更新时间:2023-01-17 11:38:33

更新06/22/2018

Update 06/22/2018

在涉及多部分请求时,请注意,Spring Boot和Spring MVC的工作方式有所不同.默认情况下,Spring MVC不会提供 ,因此无法解析多部分请求.您必须在ApplicationContext中提供一个"multipartResolver" bean.但是,Spring MVC确实为此提供了两种实现. StandardServletMultipartResolver CommonsMultipartResolver .在您的应用程序上下文中配置其中任何一个都可以.例如:

When it comes to multipart requests note that Spring Boot and Spring MVC work differently. By default Spring MVC does not provide a MultipartResolver and therefore cannot resolve multipart requests. You have to provide a "multipartResolver" bean in your ApplicationContext. However, Spring MVC does provide two implementations for this; StandardServletMultipartResolver and CommonsMultipartResolver. Configuring either of these into your application context will work. For example:

@Configuration
public class SomeConfig {
   @Bean
   public MultipartResolver multipartResolver() {
      return new CommonsMultipartResolver();
   }
}

注意:bean的名称很重要.

NB: the name of the bean is important.

另一方面,Spring Boot将代表您自动配置 StandardServletMultipartResolver 的实例,因此能够立即"解决多部分请求.

On the other hand Spring Boot will autoconfigure an instance of StandardServletMultipartResolver on your behalf and is therefore able to resolve multipart requests "out of the box".

原始答案

您也应该使用FormData对象传递其他数据.

You should pass the additional data using the FormData object too.

更改您的客户端代码以附加其他数据:

Change your client-side code to append the additional data:

myApp.controller('passJSONTestController', function ($rootScope, $scope, MyService) {
     $scope.submitdata=function(){
        $scope.myJSONData = [
            {   'name':$scope.name,
                'sinNumber': $scope.sinNo,
                'status': $scope.status,
                'message':$scope.message,
            }
        ];
        var fd=new FormData();
        angular.forEach($scope.files,function(file){
            console.log("file " + file);
            fd.append('file',file);
        });

        // do this instead
        data.append('json', JSON.stringify($scope.myJSONData);
        ...

在您的控制器中修复请求映射,并使其接受 MultipartHttpServletRequest 参数而不是 List< MultipartFile> :

In your controller fix the request mapping and make it accept MultipartHttpServletRequest parameter instead of List<MultipartFile>:

@Controller
@RequestMapping("/dataStack")
public class GetData {
 @RequestMapping(value = "/getAllDataInfo", method = RequestMethod.POST)
    public
    @ResponseBody
    String sendInfo(MultipartHttpServletRequest req){        

       List<MultipartFile> multiPartFileList = req.getFiles("file");
       ...

       String[] json = req.getParameterMap().get("json");

       // Jackson deserialization...but you could use any...Gson, etc
       ObjectMapper mapper = new ObjectMapper();
       TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
       Map<String,String> data = mapper.readValue(json[0], typeRef);
       ...

确保更新MyService以便发布到新的请求映射:

Make sure you update MyService to post to the new request mapping:

myService.sendJSON = function (fd) {
         var deferred = $q.defer();
         var myUrl = applnURL + '/dataStack/getAllDataInfo.form';
        ...

我认为应该为您做.

HTH