使用单个请求发送JSON和图像。(Send JSON and Image with single request. Angular + Spring Boot)
弹簧控制器
@PostMapping( value = "/post", produces = MediaType.APPLICATION_JSON_VALUE, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE} ) public ResponseEntity<User> handleFileUpload(@RequestParam("user") User user, @RequestPart("file") MultipartFile file) { // do something with User and file return ResponseEntity.ok().build(); }
角度服务
@Injectable() export class UploadFileService { constructor(private http: HttpClient) { } pushFileToStorage(file: File): Observable<HttpEvent<{}>> { let formdata: FormData = new FormData(); formdata.append('file', file); formdata.append('user', JSON.stringify(new User('John', 12))) const req = new HttpRequest('POST', '/post', formdata, { reportProgress: true, }); return this.http.request(req); } }
当我尝试发送请求时,我得到
500 Internal Server Error.
这是一个请求标题
POST /post HTTP/1.1 Host: localhost:4200 Connection: keep-alive Content-Length: 152881 Accept: application/json, text/plain, */* Content-Type: multipart/form-data; boundary=----WebKitFormBoundarydaQb5yaWw2xu1V9r Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9
请求负载
------WebKitFormBoundarydaQb5yaWw2xu1V9r Content-Disposition: form-data; name="file"; filename="Screen Shot 2017-10-24 at 8.49.13 PM.png" Content-Type: image/png ------WebKitFormBoundarydaQb5yaWw2xu1V9r Content-Disposition: form-data; name="user" {"name":"John","age":12} ------WebKitFormBoundarydaQb5yaWw2xu1V9r--
注意:如果在Spring rest控制器中,我将参数类型User更改为String,它将起作用。
问题:如何从Angular发送请求,以便在Spring上可以获取User和MultipartFile,而不是String。
Spring Rest Controller
@PostMapping( value = "/post", produces = MediaType.APPLICATION_JSON_VALUE, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE} ) public ResponseEntity<User> handleFileUpload(@RequestParam("user") User user, @RequestPart("file") MultipartFile file) { // do something with User and file return ResponseEntity.ok().build(); }
Angular Service
@Injectable() export class UploadFileService { constructor(private http: HttpClient) { } pushFileToStorage(file: File): Observable<HttpEvent<{}>> { let formdata: FormData = new FormData(); formdata.append('file', file); formdata.append('user', JSON.stringify(new User('John', 12))) const req = new HttpRequest('POST', '/post', formdata, { reportProgress: true, }); return this.http.request(req); } }
When I try to send the request I get
500 Internal Server Error.
Here's a request header
POST /post HTTP/1.1 Host: localhost:4200 Connection: keep-alive Content-Length: 152881 Accept: application/json, text/plain, */* Content-Type: multipart/form-data; boundary=----WebKitFormBoundarydaQb5yaWw2xu1V9r Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9
Request payload
------WebKitFormBoundarydaQb5yaWw2xu1V9r Content-Disposition: form-data; name="file"; filename="Screen Shot 2017-10-24 at 8.49.13 PM.png" Content-Type: image/png ------WebKitFormBoundarydaQb5yaWw2xu1V9r Content-Disposition: form-data; name="user" {"name":"John","age":12} ------WebKitFormBoundarydaQb5yaWw2xu1V9r--
Note: If in Spring rest controller I change parameter type User to String it works.
Question: How to send request from Angular so that on Spring I can get User and MultipartFile, instead of String.
原文:https://stackoverflow.com/questions/47756044
更新时间:2023-06-11 18:06
最满意答案
在SQL Server中
SELECT PK, account, sum_value AS [value] FROM ( SELECT PK, account, SUM([value]) as sum_value, ROW_NUMBER() OVER (PARTITION BY PK ORDER BY SUM([value]) DESC) as rn FROM [table] GROUP BY account, PK ) as p WHERE rn = 1
输出:
PK account value 1 40010101 630 2 40010569 300
In SQL Server
SELECT PK, account, sum_value AS [value] FROM ( SELECT PK, account, SUM([value]) as sum_value, ROW_NUMBER() OVER (PARTITION BY PK ORDER BY SUM([value]) DESC) as rn FROM [table] GROUP BY account, PK ) as p WHERE rn = 1
Output:
PK account value 1 40010101 630 2 40010569 300
相关问答
更多-
这是使用UNION ALL (参见SQL Fiddle with Demo )的一种方法。 这适用于两个组,如果您有两个以上的组,则需要指定group号并为每个group添加查询: ( select * from mytable where `group` = 1 order by age desc LIMIT 2 ) UNION ALL ( select * from mytable where `group` = 2 order by age desc LIM ...
-
在mysql中有一个超简单的方法: select * from (select * from mytable order by `Group`, age desc, Person) x group by `Group` 这是因为在mysql中,您不允许聚合非分组列,在这种情况下,mysql只返回第一行。 解决方案是首先对数据进行排序,以便对于每个组,您想要的行是第一个,然后按您想要的值分组。 您避免复杂的子查询尝试找到max()等,以及当有多个具有相同最大值的返回多行时的问题(如其他答案) 注意:这是一 ...
-
尝试使用NOT EXISTS() : SELECT s.* FROM ( SELECT strftime('%W', date(p.match_date, 'unixepoch', 'localtime')) AS week_number, sum(red_cards + yellow_cards) AS cards, user_id FROM user_records GROUP BY week_number, user_id) s WHERE ...
-
分组的不同值的总和(Sum of the grouped distinct values)[2023-06-08]
好的,我通过在SQL查询中添加一个ROW_NUMBER()函数来解决这个问题: SELECT Group, ParentID, ROW_NUMBER() OVER (PARTITION BY ParentID ORDER BY ChildID ASC) AS Position, ChildID, ParentProdCount FROM Table 然后我用。替换了SSRS SUM函数 =SUM(IIF(Position = 1, ParentProdCount.Value, 0)) Ok I got ... -
您不能在JPQL中的FROM块中使用子查询,只允许SELECT和WHERE块。 尝试像这样: SELECT m FROM Myclass m WHERE m.date_of_creation IN ( SELECT MAX(mm.date_of_creation) FROM Myclass mm WHERE mm.id = m.id) PS Code未经测试 You can't use subqueries in FROM block in JPQL, only SELECT and W ...
-
有一些事情非常令人困惑或需要澄清: 您的标签是Sql-Server ,但您向我们提供了MySql示例 似乎没有必要使用派生表,事实上它使它有点混乱。 话虽如此,如果您希望MAX ContractPrice具有MAX ID可以通过多种方式实现。 还请注意我没有正确地聚合其余的列,这是不好的做法。 只需将尚未聚合或分组的其余列包装在MAX()中: SELECT t1.MeasurableID, t1.EntityID, MAX(t1.contractprice) AS MAXprice ...
-
如何查询分组记录的最大数量并显示该日期(How to query the max quantity of grouped records and display that date)[2021-03-05]
只使用一种方法而不使用max。 SQL小提琴演示 select top 1 item, quantity, date from vwArchiveQuantityByLoadDate where Item = 'I1' order by quantity desc, date desc Just one way without using max. SQL Fiddle Demo select top 1 item, quantity, date from vwArchiveQuantityByLoadD ... -
Rails - 在已经对结果进行分组后对列进行求和(Rails - Sum a column after having already grouped the results)[2022-07-15]
然后将值相加 results.sum(:col_name).values.sum Just sum the values then results.sum(:col_name).values.sum -
只需使用窗口功能: SELECT c.* FROM (SELECT c.*, SUM(count) OVER (PARTITION BY index) as cnt, ROW_NUMBER() OVER (PARTITION BY index ORDER BY count DESC) as seqnum FROM counts c ) c WHERE seqnum = 1; 这假设您要保留其他列。 如果您只想要每个index的最大值和总和: SELECT i ...
-
在SQL Server中 SELECT PK, account, sum_value AS [value] FROM ( SELECT PK, account, SUM([value]) as sum_value, ROW_NUMBER() OVER (PARTITION BY PK ORDER BY SUM([value]) DESC) as rn FROM [table] ...