InputStream gives null data in MessageBodyReader Jersey

I'm trying to read APPLICATION_FORM_URLENCODEDin MessageBodyReaderon jersey. The stream returns null data when I try to read it usingBufferedReader

Here is my code:

@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public class EmployeeReader implements MessageBodyReader<Employee> {

    @Override
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, 
        MediaType mediaType) {
        return true;
    }

    @Override
    public Employee readFrom(Class<Employee> type, Type genericType, Annotation[] annotations, 
        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) 
        throws IOException, WebApplicationException {
        System.out.println(entityStream.available());  //Prints 0
        BufferedReader br = new BufferedReader(new InputStreamReader(entityStream));
        String data = br.readLine();
        System.out.println("Stream Read:"+data);
        //data is null here
        .....
    }
}

I see that the data is being sent from my form to the POST request as application/x-www-form-urlencoded, however I cannot read it in MessageBodyReader.

During debugging, I see that ByteChunk contains the following data:

POST /Employees/employee HTTP/1.1
host:localhost:80800
connection:keep-alivee
content-length:144
postman-token:cf873d98-3208-292c-8fc1-6da8138a31faa
cache-control:no-cachee
origin:chrome-extension://fhbjgbiflinjbdggehcddcbncdddomopp
user-agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.366
content-type:application/x-www-form-urlencodedd
accept:*/**
accept-encoding:gzip, deflate, brr
accept-language:en-US,en;q=0.88

id=3&name=Test

UPDATE

I just found out that this is some kind of side effect SpringBootServletInitializer. Disabling this result causes the above code to work fine.

Does anyone help?

+4
source share
1 answer

-, HiddenHttpMethodFilter, getParameter, POST. , InputStream br.readLine(); null.

@Configuration
public class FilterConfig  {
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                    FilterChain filterChain) throws ServletException, IOException {
                if ("POST".equals(request.getMethod())
                        && request.getContentType().equals(MediaType.APPLICATION_FORM_URLENCODED)) {
                    //Skip this filter and call the next filter in the chain.
                    filterChain.doFilter(request, response);
                } else {
                    //Continue with processing this filter.
                    super.doFilterInternal(request, response, filterChain);
                }
            }
        };
    }
}

, , , POST, MediaType APPLICATION_FORM_URLENCODED, , , .

, InputStream , MessageBodyReader

+4

Source: https://habr.com/ru/post/1662302/


All Articles