本文介绍了如何添加HTTP标头到soap webservice glassfish的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的webservice类:



<$>我如何使用netbeans和glassfish将http头添加到soap webservice? p $ p> package com.service;

import com.model.Contact;
import com.model.Phonebook;
import java.util.ArrayList;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


@WebService(serviceName =PhonebookService)
public class PhonebookService {

private Phonebook phonebook = new Phonebook();

@WebMethod(operationName =insert)
公共字符串插入(@WebParam(name =contact)联系人联系人){

phonebook.add(联系);
返回contact.getName()+插入;


$ b @WebMethod(operationName =update)
public String update(@WebParam(name =contact)联系人联系人){

phonebook.update(contact);
return contact.getName()+updated;


$ b @WebMethod(operationName =remove)
public String remove(@WebParam(name =contact)联系人联系人){

phonebook.remove(contact);
return contact.getName()+removed;



@WebMethod(operationName =list)
public ArrayList< Contact> list(){

return phonebook.list();

}

}

我想添加Access-Control-Allow-Origin:*标题



我该如何做?

解决方案

通过互联网搜索几个小时,我编写了下面的代码,允许添加额外的http头文件。我使用netbeans生成的webservice和内置的glassfish 3服务器

  import java.io. *; 
import javax.servlet。*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http。*;

@WebFilter(urlPatterns = {/ *})
public class CrossOriginResourceSharingFilter implements Filter {

public CrossOriginResourceSharingFilter(){}

@Override
public void init(FilterConfig fConfig)throws ServletException {}
$ b @Override
public void destroy(){}

@Override
public void doFilter(
ServletRequest request,ServletResponse response,
FilterChain chain)throws IOException,ServletException {
$ b $((HttpServletResponse)响应).addHeader(
Access-Control-Allow-Origin,*
);
((HttpServletResponse)response).addHeader(
Access-Control-Allow-Headers,Content-Type,Authorization,Accept
);
((HttpServletResponse)response).addHeader(
Access-Control-Allow-Methods,GET,POST,OPTIONS
);

chain.doFilter(request,response);
}
}


How can I add http header to soap webservice using netbeans and glassfish?

My webservice class:

package com.service;

import com.model.Contact;
import com.model.Phonebook;
import java.util.ArrayList;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


@WebService(serviceName = "PhonebookService")
public class PhonebookService {

    private Phonebook phonebook = new Phonebook();

    @WebMethod(operationName = "insert")
    public String insert(@WebParam(name = "contact") Contact contact) {

        phonebook.add(contact);
        return contact.getName() + " inserted";

    }

    @WebMethod(operationName = "update")
    public String update(@WebParam(name = "contact") Contact contact) {

        phonebook.update(contact);
        return contact.getName() + " updated";

    }

    @WebMethod(operationName = "remove")
    public String remove(@WebParam(name = "contact") Contact contact) {

        phonebook.remove(contact);
        return contact.getName() + " removed";

    }

    @WebMethod(operationName = "list")
    public ArrayList<Contact> list() {

        return phonebook.list();

    }

}

I would like to add "Access-Control-Allow-Origin: *" header

How can I do it?

解决方案

Searching over internet for hours, I ended up coding the following code which allow to add extra http headers. I am using it with netbeans generated webservice and built-in glassfish 3 server

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;

@WebFilter(urlPatterns = { "/*" })
public class CrossOriginResourceSharingFilter implements Filter {

    public CrossOriginResourceSharingFilter() { }

    @Override
    public void init(FilterConfig fConfig) throws ServletException { }

    @Override
    public void destroy() { }

    @Override
    public void doFilter(
        ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {

        ((HttpServletResponse)response).addHeader(
            "Access-Control-Allow-Origin", "*"
        );
                ((HttpServletResponse)response).addHeader(
            "Access-Control-Allow-Headers", "Content-Type, Authorization, Accept"
        );
                ((HttpServletResponse)response).addHeader(
                        "Access-Control-Allow-Methods", "GET, POST, OPTIONS"
                );

        chain.doFilter(request, response);
    }
}

这篇关于如何添加HTTP标头到soap webservice glassfish的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:35