2

I have a list of integers at the client side controller as :

selectedSegments = [19,26]

Now, I need to pass this list from client side to the back end hence I am calling the back end service as :

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 );
             }
         );

And service (java class) side I have :

@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 );
    }

}

This class is working fine with other functions and with other type of params but when I am trying to pass a list as param I am getting 500 error with the error message:

Missing URI template variable 'segmentIds' for method parameter of type List

MAy I get help on how to pass param as list? Please let me know should I provide more information

2 Answers 2

2

You need to pass it as Query params ( as it is part of the Path Variable ) and not as part of request body ( 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 (

And at the controller level, you will need to modify

@PathVariable( "segmentIds" ) List<Integer> segmentIds

to

@PathVariable( "segmentIds" ) String segmentIds

And then extract the list from the string that is passed in.

Sign up to request clarification or add additional context in comments.

Comments

1

when dealing with java backend, avoid as much as possible to use lists as top level transfer objects.

usually jersey or any other runtime will have trouble figuring out a suitable list implementation to use.

try approaches like this one instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.