我在客户端控制器上有一个整数列表:
selectedSegments = [19,26]
现在,我需要将此列表从客户端传递到后端因此我将后端服务称为:
return $http.post( 'dde/segments/deleteSegmentsForSelectedClient' ,{segmentIds: segmentIds } )
.then
(
function( response )
{
return response.data;
},
function( errResponse )
{
console.error( ' Error while getting results of query' + errResponse);
return $q.reject( errResponse );
}
);
我有服务(java类)方面:
@RequestMapping( "/dde" )
@RestController
public class SegmentController
{
@Autowired
@Qualifier( "apiSegmentService" )
private SegmentService segmentService;
private static final Logger LOG = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() );
@RequestMapping( value = "/segments/deleteSegmentsForSelectedClient", method = RequestMethod.POST )
@Transactional(transactionManager = "apiTransactionManager", propagation = Propagation.REQUIRED)
public ResponseEntity<Void> deleteSegmentsForSelectedClient( @PathVariable( "segmentIds" ) List<Integer> segmentIds )
{
for(int id: segmentIds)
{
try
{
segmentService.deleteSegmentEntity( id );
}
catch ( ParserException e )
{
return new ResponseEntity<>( HttpStatus.NO_CONTENT );
}
}
return new ResponseEntity<>( HttpStatus.OK );
}
}
这个类适用于其他函数和其他类型的参数,但当我尝试将列表作为参数传递时,我收到500错误并显示错误消息:
Missing URI template variable 'segmentIds' for method parameter of type List
我可以获得有关如何将param作为列表传递的帮助吗?如果我提供更多信息,请告诉我
答案 0 :(得分:2)
您需要将其作为查询参数传递(因为它是Path Variable
的一部分)而不是请求正文的一部分(which is the case in your code
)
let source = 'dde/segments/deleteSegmentsForSelectedClient';
// construct the segmentIds key value
let params = 'segmentIds=' + segmentIds.join(',');
// append the params to the url
let url = [source, params].join('?');
$http.post( url)
.then (
在控制器级别,您需要修改
@PathVariable( "segmentIds" ) List<Integer> segmentIds
到
@PathVariable( "segmentIds" ) String segmentIds
然后从传入的字符串中提取列表。
答案 1 :(得分:1)