Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JAX-WS webservice respond with specific http code

Just like the title says.

    @WebService(
        targetNamespace = "http://com.lalaland.TestWs",
        portName = "TestWs",
        serviceName = "TestWs")
public class TestWs implements TestWsInterface {

    @EJB(name="validator")
    private ValidatorLocal validator;

    @WebMethod(operationName = "getStuff")
    public List<StuffItem> getStuff(@WebParam(name = "aaa")String aaa, 
                        @WebParam(name = "bbb")int bbb )  {

          if ( ! validator.check1(...) ) 
               return HTTP code 403        <------------ Here
          if ( ! validator.check2(...) )
               return HTTP code 404        <------------ Here
          if ( ! validator.check3(...) ) 
               return HTTP code 499        <------------ Here

          return good list of Stuff Items

    }

Is there anyway I can make a method return a specific HTTP code on demand? I know that some of the stuff, like authentication, internal server errors , etc make the the WS method return 500 and auth errors , but I would like to be able to send these in accordance with by business logic.

Anyone done this before? Been using jax-WS for some time and this was the first time I had this need, tried searching for it and couldn't find an answer anywhere.

Thanks

like image 591
SysHex Avatar asked Oct 10 '13 13:10

SysHex


People also ask

How do I create a JAX-WS web service?

Click Open Project. In the Projects tab, right-click the helloservice-war project and select Run. This command builds and packages the application into a WAR file, helloservice-war. war , located in tut-install/examples/jaxws/helloservice-war/target/ , and deploys this WAR file to your GlassFish Server instance.

How JAX-WS is useful for describing SOAP web services?

JAX-WS is a technology for building web services and clients that communicate using XML. JAX-WS allows developers to write message-oriented as well as RPC-oriented web services. In JAX-WS, a web service operation invocation is represented by an XML-based protocol such as SOAP.

Which annotation is used to create a web service endpoint?

WebService annotation. The WebService annotation defines the class as a web service endpoint. A service endpoint interface (SEI) is a Java interface that declares the methods that a client can invoke on the service.

Which annotation is used to create a web service endpoint that is 50?

The @WebService annotation defines the class as a web service endpoint.


2 Answers

Only get the current instance of javax.servlet.http.HttpServletResponse and sends the error.

@WebService
public class Test {

    private static final Logger LOG = Logger.getLogger(Test.class.getName());

    @Resource
    private WebServiceContext context;

    @WebMethod(operationName = "testCode")
    public String testCode(@WebParam(name = "code") int code) {
        if (code < 200 || code > 299) {
            try {
                MessageContext ctx = context.getMessageContext();
                HttpServletResponse response = (HttpServletResponse) 
                        ctx.get(MessageContext.SERVLET_RESPONSE);
                response.sendError(code, code + " You want it!");
            } catch (IOException e) {
                LOG.severe("Never happens, or yes?");
            }
        }
        return code + " Everything is fine!";
    }

}

See also List of HTTP status codes - Wikipedia, the free encyclopedia

like image 102
Paul Vargas Avatar answered Nov 03 '22 01:11

Paul Vargas


Try this:

Create a SoapHandler like this: http://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-server-side/ implementing the interface: Handler.handleResponse();

then, inside the handler you are avalaible to modify as you like the http headers, so you can add something like: http://download.java.net/jdk7/archive/b123/docs/api/javax/xml/ws/handler/MessageContext.html

Where you can use the: HTTP_RESPONSE_CODE as you want.

Other resource: http://docs.oracle.com/cd/E14571_01/web.1111/e13735/handlers.htm

Tip: think on soaphandlers as interceptors for soap messages

like image 39
Sergio Avatar answered Nov 02 '22 23:11

Sergio