在/ voterRestService上已经运行了一个端点

时间:2019-02-14 07:06:53

标签: jax-rs cxf

我是Web服务的新手。我出于示例目的创建了一个简单的服务,每当我启动服务器时,都会出现以下错误。请任何人帮助我摆脱这个错误。

/ voterRestService上已经有一个端点在运行

Voter.java

    package com.swagger.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Voter")
public class Voter
{
    private long id;
    private String name;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

VoterService.java

    package com.swagger.service;

import javax.jws.WebService;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import com.swagger.model.Voter;

import io.swagger.annotations.Api;

@Api(value="Voter Service", description="Voter Rest Service")
@Path("/voterService")
@Produces({"application/xml"})
@Consumes({"application/xml"})
@WebService
public interface VoterService 
{
    @GET
    @Path("/voters")
    Response getVoters();

    @GET
    @Path("/voters/{id}")
    Response getVoterId(@PathParam("id") String id);

    @POST
    @Path("/voters")
    Response addVoter(Voter voter);

    @PUT
    @Path("/voters")
    Response updateVoter(Voter voter);

    @DELETE
    @Path("/voters")
    Response deleteVoterById(@QueryParam("id") String id);
}

VoterServiceImpl.java

    package com.swagger.service;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;
import javax.ws.rs.core.Response;

import com.swagger.model.Voter;

@WebService(endpointInterface="com.swagger.service.VoterService")
public class VoterServiceImpl implements VoterService
{
    private Map<Long,Voter> voters=new HashMap<Long,Voter>();
    private long currentId = 0;

    public VoterServiceImpl()
    {
        init();
    }

    public void init()
    {
        Voter voter1=new Voter();
        voter1.setId(++currentId);
        voter1.setName("Sandeep Reddy");

        Voter voter2=new Voter();
        voter2.setId(++currentId);
        voter2.setName("Mithali Raj");

        voters.put(voter1.getId(), voter1);
        voters.put(voter2.getId(), voter2);
    }

    @Override
    public Response getVoters() 
    {
        if(!voters.isEmpty())
        {
            return Response.ok(voters).build();
        }
        else
        {
            return Response.noContent().build();
        }
    }

    @Override
    public Response getVoterId(String id)
    {
        long voterId=Long.parseLong(id);
        Voter voter = voters.get(voterId);
        if(voter != null)
        {
            return Response.ok(voter).build();
        }
        else
        {
            return Response.noContent().build();
        }
    }

    @Override
    public Response addVoter(Voter voter) 
    {
        if(voter != null)
        {
        voters.put(++currentId, voter);
        return Response.ok(voter).build() ;
        }
        else
        {
            return Response.notModified("Give valid details to add").build();
        }
    }

    @Override
    public Response updateVoter(Voter voter) 
    {
        Voter voterToUpdate = voters.get(voter.getId());
        if(voterToUpdate != null)
        {
            voters.put(voter.getId(), voter);
            return Response.ok(voter).build();
        }
        else
        {
            return Response.notModified().build();
        }
    }

    @Override
    public Response deleteVoterById(String id) 
    {
        long voterId = Long.parseLong(id);
        Voter voter=voters.get(voterId);
        if(voter != null)
        {
            return Response.ok().build();
        }
        else
        {
            return Response.notModified().build();
        }
    }
}

web.xml

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/cxf-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services</url-pattern>
    </servlet-mapping>

</web-app>

cxf-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://cxf.apache.org/configuration/beans"
    xmlns:configuration="http://cxf.apache.org/transports/http/configuration"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:core="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://cxf.apache.org/configuration/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/configuration/beans http://cxf.apache.org/schemas/configuration/cxf-beans.xsd
        http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd">

    <bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
        <property name="resourcePackage" value="com.swagger" />
    </bean>

    <bean id="voterService" class="com.swagger.service.VoterServiceImpl"></bean>

    <jaxrs:server id="serviceBean" address="/voterRestService">
        <jaxrs:serviceBeans>
            <ref bean="voterService" />
        </jaxrs:serviceBeans>
        <jaxrs:features>
            <ref bean="swagger2Feature" />
        </jaxrs:features>
    </jaxrs:server>

</beans>

enter image description here

请任何人帮助我摆脱此错误。我在过去4个小时内一直在尝试,但尚未找到任何解决方案。

1 个答案:

答案 0 :(得分:0)

您不需要在地址和@Path中声明两次“ / voterRestService”。另外@WebService批注用于肥皂服务,而不用于rest api。