0

我正在尝试基于用户上传的 XML 文件来实现布尔返回(真/假)。例如,我有一个元素方向,指示它包含的数据类型。所以我有兴趣取消编组数据并返回布尔值。

第 1 步:对 POST 方法感兴趣,将使用POSTMAN chrome 应用程序进行测试。

第 2 步:Contents 对象保存所有用于取消编组和编组的内容

包验证器.服务;

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

1 回答 1

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();
  }
}
于 2012-05-11T09:17:54.573 回答