0

我有一个用于在数据库中添加项目的 Rest Web 服务。架构如下:

从 UI 生成的请求,其中包含 JSON 格式的项目以在 DB 中添加项目 --> 控制器:将 JSON 转换为 XML 以用于 rest web 服务 --> Rest web 服务:获取 XML 并提取数据并添加到 db 中。

现在...从 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