gpt4 book ai didi

java - 如何将http header 添加到soap webservice glassfish

转载 作者:行者123 更新时间:2023-12-01 15:10:55 33 4
gpt4 key购买 nike

如何使用 netbeans 和 glassfish 将 http header 添加到 SOAP Web 服务?

我的网络服务类:

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();

}

}

我想添加“Access-Control-Allow-Origin: *” header

我该怎么做?

最佳答案

在互联网上搜索了几个小时,我最终编写了以下代码,该代码允许添加额外的 http header 。我将它与 Netbeans 生成的 Web 服务和内置 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 { }

@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);
}
}

关于java - 如何将http header 添加到soap webservice glassfish,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379682/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com