GET Request with Content-Type and Accept header with JAX-RS Jersey 2.2

前端 未结 3 1390
盖世英雄少女心
盖世英雄少女心 2020-12-24 15:11

I try to access an open data web service which gives me traffic infos. Documentation says that requests have to be GET and need to contain Accept: applica

3条回答
  •  清歌不尽
    2020-12-24 15:35

    You can use the ContainerResponseFilter to set the default Accept header if it is not provided in the input request.

    @Provider
    public class EntityResponseFilter implements ContainerResponseFilter {
    
        private MediaType getExternalMediaType(){
            MediaType mediaType = new MediaType("application", "vnd.xxx.resource+json")
            return mediaType; 
        }
    
        @Override
        public void filter( ContainerRequestContext reqc , ContainerResponseContext resc ) throws IOException {
            MediaType mediaType = getExternalMediaType(); 
            List mediaTypes = reqc.getAcceptableMediaTypes();
            if( mediaTypes.contains(mediaType) ) {   
                resc.setEntity( resc.getEntity(), new Annotation[0], mediaType );
            }
            // ...
        }
    }
    

提交回复
热议问题