Posting JSON to REST API

后端 未结 9 853
-上瘾入骨i
-上瘾入骨i 2020-12-08 08:12

I\'m creating a REST API that will accept JSON requests.

I\'m testing it out using CURL:

curl -i -POST -H \'Accept: application/json\' -d \'{\"id\":1         


        
9条回答
  •  无人及你
    2020-12-08 09:05

    Its 2014 and I wanted to add a few updates to this question which helped me solve the same problem.

    1. Code update to replace deprecated AnnotationMethodHandlerAdapter in Spring 3.2

          @Configuration
          public class AppConfig {
      
      
          @Bean
          public RequestMappingHandlerAdapter  annotationMethodHandlerAdapter()
          {
              final RequestMappingHandlerAdapter annotationMethodHandlerAdapter = new RequestMappingHandlerAdapter();
              final MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
      
              List> httpMessageConverter = new ArrayList>();
              httpMessageConverter.add(mappingJacksonHttpMessageConverter);
      
              String[] supportedHttpMethods = { "POST", "GET", "HEAD" };
      
              annotationMethodHandlerAdapter.setMessageConverters(httpMessageConverter);
              annotationMethodHandlerAdapter.setSupportedMethods(supportedHttpMethods);
      
              return annotationMethodHandlerAdapter;
          }
      }
      
    2. HTTP/1.1 415 Unsupported Media Type error

    After spending many hours trying to figure out why I am STILL GETTING a 415 error even after adding the correct JSON configuration I finally realized that the problem was NOT with the server side but with the client side. In order for Spring to accept your JSON you MUST make sure that you are sending both "Content-Type : application/json" and "Accept: application/json" as part of your http headers. for me specifically it was an android application HttpUrlConnection which I had to set as:

        public static String doPost(final String urlString,final String requestBodyString) throws IOException {
            final URL url = new URL(urlString);
    
            final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
              urlConnection.setReadTimeout(10000 /* milliseconds */);
              urlConnection.setConnectTimeout(15000 /* milliseconds */);
              urlConnection.setRequestProperty("Content-Type", "application/json");
              urlConnection.setRequestProperty("Accept", "application/json");
              urlConnection.setDoOutput(true);
              urlConnection.setRequestMethod("POST");
              urlConnection.setChunkedStreamingMode(0);
    
              urlConnection.connect();
    
              final PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
              out.print(requestBodyString);
              out.close();
    
              final InputStream in = new BufferedInputStream(urlConnection.getInputStream());
              final String response =  readIt(in);
    
              in.close(); //important to close the stream
    
              return response;
    
            } finally {
              urlConnection.disconnect();
            }
        }
    

提交回复
热议问题