2012-05-10 12 views
0

ユーザによるXMLファイルのアップロードに基づいてBOOLEANリターン(true/false)を達成しようとしています。たとえば、要素の方向が含まれているデータの種類を示します。だから私は、データをマーシャリングしてブール値を返すことに興味があります。JAXRSを使用した安心サービスのためのJAVAオブジェクトへのXMLのマージン

ステップ1:POSTメソッドに興味があり、POSTMANクロムアプリを使用してテストします。

ステップ2:内容は国連マーシャリングのためにすべてを保持するオブジェクトと

パッケージvalidator.serviceをマーシャリング。

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

// Created the Contents object to hold everything for un marshaling and marshaling 

@XmlRootElement(name = "contents") 
public class Contents 
{ 
    @XmlElement 
    String portalarea; 

    @XmlElement 
    String portalsubarea; 

    @XmlElement 
    String direction; 

    public String getportalarea() 
    { 
     return portalarea; 
    } 

    public String getportalsubarea() 
    { 
     return portalsubarea; 
    } 

    public String getdirection() 
    { 
     return direction; 
    } 

} 

ステップ3:リクエストを受信し、XMLを整列化して論理値を返すための検証クラスを持ちます。

package validatorService; 

import java.io.InputStream; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBElement; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.stream.XMLInputFactory; 
import javax.xml.stream.XMLStreamReader; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

@Path ("/valid") 
public class ValidatorService 
{ 
    boolean n_value = false; 
    boolean r_value = false; 

    @POST 
    @Produces(MediaType.TEXT_PLAIN) 
    @Consumes("application/xml") 
    public String validate(String xmlContent) 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 

     try 
     { 
      if (xmlContent != null) 
      { 
       if (xmlContent.startsWith("https")) 
       { 
        HttpGet xmlGet = new HttpGet(xmlContent); 

        HttpResponse response = httpclient.execute(xmlGet); 
        int responseStatus = response.getStatusLine().getStatusCode(); 
        // String responseMessage = response.getStatusLine().getReasonPhrase(); 

        if (responseStatus == 200) 
        { 
         HttpEntity responseEntity = response.getEntity(); 
         InputStream inStream = responseEntity.getContent(); 

         Contents direction = unmarshalingContent(inStream, xmlContent); 

         if (direction.equals("N")) 
         { 
          n_value = true; 


         } 
         else if (direction.equals("R")) 
         { 
          r_value = true; 


        } 
        else 
        { 
         System.out.println("Response Error : " + responseStatus); // Should be 
                        // handled 
                        // properly 
        } 

       } 
       else 
       { 
        System.out.println(" 'https' Format Error"); // Should be handled properly 
       } 

       return "success"; 
      } 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      System.out.println(" Error caught at catch " + e); // Should be handled properly for 
                   // all exception 
     } 
     finally 
     { 
      httpclient.getConnectionManager().shutdown(); 
     } 

     return null; 
    } 

    public Contents unmarshalingContent(InputStream inputStream, String resourceClass) throws Exception 
    { 
     System.out.println(" welcome "); 

     if (resourceClass == "xmlContent") 
     { 
      JAXBContext jc = JAXBContext.newInstance("com.acme.bar"); 
      Unmarshaller u = jc.createUnmarshaller(); 

      XMLInputFactory inputFactory = XMLInputFactory.newInstance(); 
      XMLStreamReader xReader = inputFactory.createXMLStreamReader(inputStream); 

      JAXBElement<Contents> jaxBElement = (JAXBElement<Contents>) u.unmarshal(xReader, Contents.class); 

      Contents portalArea = (Contents) jaxBElement.getValue(); 
      Contents portalSubarea = (Contents) jaxBElement.getValue(); 
      Contents direction = (Contents) jaxBElement.getValue(); 

      return direction; 
     } 
     throw new Exception("Invalid resource request"); 

    } 
} 

私はRESTfulサービスに新しく、いくつかのドキュメントを読み、指示に基づいて私は与えられたタスクを達成しようとしています。だから、どんな助け、訂正、指導、コードも大歓迎です。

答えて

4

はるかに簡単です。 XMLからJavaへのオブジェクト変換は手作業で行う必要はありません。 JAX-RSのプロバイダは自動的にこれを行います。

@POST 
@Produces(MediaType.TEXT_PLAIN) 
@Consumes("application/xml") 
public Response validate(Contents con){ //con will be initialized by JAX-RS 
    //validate your XML converted to object con 
    boolean validation_ok = ... 
    if(validation_ok){ 
    return Response.ok("true").build(); 
    }else{ 
    return Response.ok("false").build(); 
    } 
} 
関連する問題