0

データベースにアイテムを追加するために使用される Rest Web サービスがあります。アーキテクチャは次のようになります。

UI から生成された JSON 形式のアイテムを使用して DB にアイテムを追加する要求 --> コントローラー: REST Web サービス用に JSON を XML に変換します --> Rest Web サービス: XML を取得し、データを抽出して db に追加します。

今... UIから生成されたリクエストはコントローラへのgetリクエストなので、データをコントローラに渡します。

UI シミュレータ コード:

UI シミュレーター :

@RequestMapping(value = "/add", method = RequestMethod.GET)
       @ResponseBody
       public void updateAddResource() throws IOException
       {
              String url = "http://localhost:8089/PMT/people/resource/add";
              System.out.println(url);

              URL obj = new URL(url);
              HttpURLConnection con = (HttpURLConnection) obj.openConnection();
              String USER_AGENT="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
              //add request header
              con.setRequestMethod("POST");
              con.setRequestProperty("User-Agent", USER_AGENT);
              con.setRequestProperty("Content-type","application/json");
              con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

              String urlParameters = "{\"data\":{\"peopleList\":["+
                                                             "{\"employeeId\":\"8003832\",\"name\":\"rose\",\"emailId\":\"rose@virtusa.com\",\"contact\":\"9988106724\",\"designation\":\"software engineer\"},"+
                                                             "{\"employeeId\":\"8003834\",\"name\":\"ritesh\",\"emailId\":\"ritesh@virtusa.com\",\"contact\":\"99883106724\",\"designation\":\"software e3ngineer\"}]}}";

              // Send post request
              con.setDoOutput(true);
              DataOutputStream wr = new DataOutputStream(con.getOutputStream());
              wr.writeBytes(urlParameters);
              wr.flush();
              wr.close();

              int responseCode = con.getResponseCode();
              System.out.println("\nSending 'POST' request to URL : " + url);
              System.out.println("Post parameters : " + urlParameters);
              System.out.println("Response Code : " + responseCode);

              BufferedReader in = new BufferedReader(
                      new InputStreamReader(con.getInputStream()));
              String inputLine;
              StringBuffer response = new StringBuffer();

              while ((inputLine = in.readLine()) != null) {
                     response.append(inputLine);
              }
              in.close();

              //print result
              System.out.println(response.toString());
       }

コントローラ:

@RequestMapping(value = "/resource/add", method = RequestMethod.POST)
       @ResponseBody
       public String addResource(@RequestBody String json)
       {

              String addResult="";
              String errorMsg="FAILED";
              try{
                     System.out.println(json);
                     Gson gson = new Gson();
                     RequestVO object = gson.fromJson(json, RequestVO.class);
                     System.out.println(object);

                     XStream xstream = new XStream();
                     xstream.autodetectAnnotations(true);
                     String requestXml = xstream.toXML(object);
                     System.out.println(requestXml);

                     String url = "http://ipAddress:8087/PMT/service/resource/add;
                     log.info("Accessing addresource webservice url: '"+url+"' in addResource(@RequestBody String json) of com.jpmc.pmt.controller.TestController");        
                     String USER_AGENT="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
                     HttpClient client = new DefaultHttpClient();
                     HttpPost post = new HttpPost(url);
                     post.addHeader("User-Agent", USER_AGENT);
                     post.addHeader("Content-Type", "text/xml");
                     post.setHeader("User-Agent", USER_AGENT);
                     post.setHeader("Content-Type", "text/xml");
                     StringEntity entity = new StringEntity(requestXml, "text/xml",HTTP.DEFAULT_CONTENT_CHARSET);
                     post.setEntity(entity);           
                     HttpResponse response = client.execute(post);
                     System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

                     BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); 
                     String output;
                     System.out.println("Output from Server .... \n");
                     while ((output = br.readLine()) != null) {
                           addResult+=output;
                     }      
                     log.info("Exiting addResource(@RequestBody String json) of com.jpmc.pmt.controller.TestController");
                     return addResult;
              }catch (Exception e) { 
                     log.info("Exception: "+e.getMessage()+" caught in addResource(@RequestBody String json) of com.jpmc.pmt.controller.TestController");
                     e.printStackTrace();
                     return errorMsg;
              } 
       }

ウェブサービス:

@RequestMapping(method=RequestMethod.POST, value="/resource/add",headers = {"content-type=application/xml","Access-Control-Allow-Methods=POST, GET, PUT, UPDATE, OPTIONS"})
    public @ResponseBody String addResource(@RequestBody String body) {
        System.out.println("POST");
        System.out.println("The Request Body is "+body);
//TO ADD INTO DB
}

今、私はエラーが発生しています:

Request method 'POST' not supported. The specified HTTP method is not allowed for the requested resource (Request method 'POST' not supported).
4

0 に答える 0