java - 使用 apache cxf 的 Rest websevice 响应为空

标签 java web-services rest cxf

我已经使用 Apache CXF 尝试了 rest webservice。我正在执行创建操作。我能够创建,虽然以 json 和 xml 格式返回响应,但我没有获得任何值(value)。我的服务和服务实现如下。 服务等级:

@Path("/location/")
@WebService
public interface LocationService {

    @WebMethod
    @POST
    @Path("location")
    @Produces({"application/xml","application/json"})
    @Descriptions({
        @Description(value = "stores a new location data", target = DocTarget.METHOD),
        @Description(value = "the newly created location data", target = DocTarget.RETURN)
    })
    public LocationData createLocation(@Valid LocationData locationData) throws DuplicateLocationException;

}

服务实现:

@Service("locationService")
public class LocationServiceEndpoint implements LocationService {

    @Override
    public LocationData createLocation(LocationData locationData) {

        setNewID(locationData);
        return locationData;
    }

    private void setNewID(LocationData locationData) {
        // setting the ID
        String id = UUID.randomUUID().toString();
        locationData.setId(id);

    }
}

bean 类:

@XmlRootElement(name = "LocationData")
public class LocationData {

    private String id;
    @DateTimeFormat(pattern="yyyy-mm-dd")
    private Date date;
    @NotNull
    private String timezone;
    @NotNull
    @Size(max = 20, min = 5)
    private String location;

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getLocation() {
        return location;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb, Locale.US);
        formatter.format("ID:%s\nLocation:%s\nDate:%s\nTime zone:%s\n", getId(), getLocation(), getDate(), getTimezone());

        return sb.toString();
    }
}

我的cxf.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:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cxf="http://cxf.apache.org/core"
       xsi:schemaLocation="
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">


     <!-- REST -->
     <import resource="classpath:META-INF/cxf/cxf.xml" />
     <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml" />

     <!-- SOAP -->
     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


    <context:component-scan base-package="org.exampledriven.cxfexample"/>

    <!-- PROVIDERS -->

    <bean id="wadlProvider" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
        <property name="applicationTitle" value="CXF Test sample application" />
    </bean>

    <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
        <property name="marshallerProperties" ref="propertiesMap" />
    </bean>

    <util:map id="propertiesMap">
        <entry key="jaxb.formatted.output">
            <value type="java.lang.Boolean">true</value>
        </entry>
    </util:map>

    <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider">
        <property name="namespaceMap" ref="jsonNamespaceMap" />
    </bean>

    <util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
        <entry key="http://www.example.org/books" value="b" />
    </util:map>

    <!-- REST SERVER -->

    <jaxrs:server id="restContainer" address="/rest">
        <jaxrs:providers>
            <ref bean="jaxbProvider" />
            <ref bean="jsonProvider" />
            <ref bean="wadlProvider" />
        </jaxrs:providers>
        <jaxrs:serviceBeans>
            <ref bean="locationService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="xml" value="application/xml" />
            <entry key="json" value="application/json" />
        </jaxrs:extensionMappings>       
    </jaxrs:server>

    <!-- SOAP SERVER -->

    <jaxws:endpoint id="location" implementor="#locationService" address="/soap" />

    <!-- CXF Message logging -->

    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus> 

    <!-- CLIENT -->
    <!--
    <jaxrs:client id="locationClient"
         address="http://localhost:8080/rest/"
         serviceClass="org.exampledriven.cxfexample.webservice.LocationService"
         inheritHeaders="true">
         <jaxrs:headers>
             <entry key="Accept" value="application/xml"/>
         </jaxrs:headers>
    </jaxrs:client> 
     --> 

    <!-- Spring Validation -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

</beans>

SOAP 服务工作正常。 Rest 不生成 XML 和 JSON 格式的数据。

请帮忙!!!

日志

2016-12-25 22:27:21,427 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 1 of 8 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No HttpSession currently exists
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - No SecurityContext was available from the HttpSession: null. A new one will be created.
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 2 of 8 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 3 of 8 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 4 of 8 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 5 of 8 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2016-12-25 22:27:21,428 DEBUG [org.springframework.security.web.authentication.AnonymousAuthenticationFilter] - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
2016-12-25 22:27:21,429 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 6 of 8 in additional filter chain; firing Filter: 'SessionManagementFilter'
2016-12-25 22:27:21,429 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 7 of 8 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2016-12-25 22:27:21,429 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json at position 8 of 8 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2016-12-25 22:27:21,429 DEBUG [org.springframework.security.web.util.AntPathRequestMatcher] - Checking match of request : '/cxf/rest/location/location.json'; against '/rest/**'
2016-12-25 22:27:21,429 DEBUG [org.springframework.security.web.access.intercept.FilterSecurityInterceptor] - Public object - authentication not attempted
2016-12-25 22:27:21,429 DEBUG [org.springframework.security.web.FilterChainProxy] - /cxf/rest/location/location.json reached end of additional filter chain; proceeding with original chain
2016-12-25 22:27:21,430 DEBUG [org.apache.cxf.transport.servlet.ServletController] - Service http request on thread: Thread[qtp1179689991-19,5,main]
2016-12-25 22:27:21,430 DEBUG [org.apache.cxf.transport.http.AbstractHTTPDestination] - Create a new message for processing
2016-12-25 22:27:21,431 DEBUG [org.apache.cxf.transport.http.Headers] - Request Headers: {Accept=[*/*], accept-encoding=[gzip, deflate, br], Accept-Language=[en-US,en;q=0.8], connection=[keep-alive], Content-Length=[0], content-type=[application/json], Host=[localhost:8080], Origin=[chrome-extension://eipdnjedkpcnlmmdfdkgfpljanehloah], User-Agent=[Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36]}
2016-12-25 22:27:21,432 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Adding interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4ee68c5b to phase pre-stream
2016-12-25 22:27:21,432 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Chain org.apache.cxf.phase.PhaseInterceptorChain@1125ee5 was created. Current flow:
  receive [LoggingInInterceptor]
  pre-stream [CertConstraintsInterceptor]
  unmarshal [JAXRSInInterceptor]
  pre-logical [OneWayProcessorInterceptor]
  invoke [ServiceInvokerInterceptor]
  post-invoke [OutgoingChainInterceptor]

2016-12-25 22:27:21,432 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.LoggingInInterceptor@56222a2f
2016-12-25 22:27:21,433 INFO [org.apache.cxf.interceptor.LoggingInInterceptor] - Inbound Message
----------------------------
ID: 8
Address: http://localhost:8080/cxf/rest/location/location.json
Encoding: ISO-8859-1
Http-Method: POST
Content-Type: application/json
Headers: {Accept=[*/*], accept-encoding=[gzip, deflate, br], Accept-Language=[en-US,en;q=0.8], connection=[keep-alive], Content-Length=[0], content-type=[application/json], Host=[localhost:8080], Origin=[chrome-extension://eipdnjedkpcnlmmdfdkgfpljanehloah], User-Agent=[Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36]}
--------------------------------------
2016-12-25 22:27:21,433 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.transport.https.CertConstraintsInterceptor@4ee68c5b
2016-12-25 22:27:21,433 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor@185755a4
2016-12-25 22:27:21,434 DEBUG [org.apache.cxf.jaxrs.utils.JAXRSUtils] - Trying to select a resource class, request path : /location/location
2016-12-25 22:27:21,434 DEBUG [org.apache.cxf.jaxrs.utils.JAXRSUtils] - Trying to select a resource operation on the resource class org.exampledriven.cxfexample.webservice.LocationServiceEndpoint
2016-12-25 22:27:21,435 DEBUG [org.apache.cxf.jaxrs.utils.JAXRSUtils] - No method match, method name : readLocation, request path : /location, method @Path : /rest, HTTP Method : POST, method HTTP Method : GET, ContentType : application/json, method @Consumes : */*,, Accept : application/json,, method @Produces : */*,.
2016-12-25 22:27:21,435 DEBUG [org.apache.cxf.jaxrs.utils.JAXRSUtils] - Resource operation createLocation may get selected
2016-12-25 22:27:21,436 DEBUG [org.apache.cxf.jaxrs.utils.JAXRSUtils] - Resource operation createLocation on the resource class org.exampledriven.cxfexample.webservice.LocationServiceEndpoint has been selected
2016-12-25 22:27:21,437 DEBUG [org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor] - Request path is: /location/location
2016-12-25 22:27:21,437 DEBUG [org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor] - Request HTTP method is: POST
2016-12-25 22:27:21,437 DEBUG [org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor] - Request contentType is: application/json
2016-12-25 22:27:21,437 DEBUG [org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor] - Accept contentType is: application/json
2016-12-25 22:27:21,437 DEBUG [org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor] - Found operation: createLocation
2016-12-25 22:27:21,515 WARN [org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper] - WebApplicationException has been caught : org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
2016-12-25 22:27:21,515 DEBUG [org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper] - org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
javax.ws.rs.WebApplicationException: javax.xml.stream.XMLStreamException: org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
    at org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:236)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1036)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:616)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:580)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:238)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:89)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:123)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:207)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:213)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:154)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:126)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:185)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:108)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:164)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:550)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1359)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1330)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:484)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:229)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:970)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:414)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:187)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:904)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:247)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
    at org.eclipse.jetty.server.Server.handle(Server.java:347)
    at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:590)
    at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1054)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:601)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:214)
    at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:411)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.stream.XMLStreamException: org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
    at org.codehaus.jettison.mapped.MappedXMLInputFactory.createXMLStreamReader(MappedXMLInputFactory.java:46)
    at org.codehaus.jettison.AbstractXMLInputFactory.createXMLStreamReader(AbstractXMLInputFactory.java:106)
    at org.codehaus.jettison.AbstractXMLInputFactory.createXMLStreamReader(AbstractXMLInputFactory.java:93)
    at org.apache.cxf.jaxrs.provider.JSONUtils.createStreamReader(JSONUtils.java:141)
    at org.apache.cxf.jaxrs.provider.JSONProvider.createReader(JSONProvider.java:257)
    at org.apache.cxf.jaxrs.provider.JSONProvider.createReader(JSONProvider.java:248)
    at org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:212)
    ... 61 more
Caused by: org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
    at org.codehaus.jettison.json.JSONTokener.syntaxError(JSONTokener.java:439)
    at org.codehaus.jettison.json.JSONObject.<init>(JSONObject.java:170)
    at org.codehaus.jettison.mapped.MappedXMLInputFactory.createXMLStreamReader(MappedXMLInputFactory.java:43)
    ... 67 more
2016-12-25 22:27:21,520 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.OneWayProcessorInterceptor@5f636f35
2016-12-25 22:27:21,520 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.ServiceInvokerInterceptor@1989edd8
2016-12-25 22:27:21,520 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.OutgoingChainInterceptor@7b6555c4
2016-12-25 22:27:21,521 DEBUG [org.apache.cxf.interceptor.OutgoingChainInterceptor] - Interceptors contributed by bus: [org.apache.cxf.interceptor.LoggingOutInterceptor@7d75751c]
2016-12-25 22:27:21,521 DEBUG [org.apache.cxf.interceptor.OutgoingChainInterceptor] - Interceptors contributed by service: []
2016-12-25 22:27:21,521 DEBUG [org.apache.cxf.interceptor.OutgoingChainInterceptor] - Interceptors contributed by endpoint: [org.apache.cxf.interceptor.MessageSenderInterceptor@18c104aa]
2016-12-25 22:27:21,522 DEBUG [org.apache.cxf.interceptor.OutgoingChainInterceptor] - Interceptors contributed by binding: [org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor@146698d5]
2016-12-25 22:27:21,522 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Chain org.apache.cxf.phase.PhaseInterceptorChain@55fb1415 was created. Current flow:
  prepare-send [MessageSenderInterceptor]
  pre-stream [LoggingOutInterceptor]
  marshal [JAXRSOutInterceptor]

2016-12-25 22:27:21,522 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.MessageSenderInterceptor@18c104aa
2016-12-25 22:27:21,523 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Adding interceptor org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor@1cea4141 to phase prepare-send-ending
2016-12-25 22:27:21,524 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Chain org.apache.cxf.phase.PhaseInterceptorChain@55fb1415 was modified. Current flow:
  prepare-send [MessageSenderInterceptor]
  pre-stream [LoggingOutInterceptor]
  marshal [JAXRSOutInterceptor]
  prepare-send-ending [MessageSenderEndingInterceptor]

2016-12-25 22:27:21,524 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.LoggingOutInterceptor@7d75751c
2016-12-25 22:27:21,524 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor@146698d5
2016-12-25 22:27:21,527 DEBUG [org.apache.cxf.phase.PhaseInterceptorChain] - Invoking handleMessage on interceptor org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor@1cea4141
2016-12-25 22:27:21,528 INFO [org.apache.cxf.interceptor.LoggingOutInterceptor] - Outbound Message
---------------------------
ID: 8
Response-Code: 500
Content-Type: text/xml
Headers: {Date=[Sun, 25 Dec 2016 16:57:21 GMT], Content-Length=[0]}
--------------------------------------
2016-12-25 22:27:21,532 DEBUG [org.apache.cxf.transport.servlet.ServletController] - Finished servicing http request on thread: Thread[qtp1179689991-19,5,main]
2016-12-25 22:27:21,532 DEBUG [org.springframework.security.web.access.ExceptionTranslationFilter] - Chain processed normally
2016-12-25 22:27:21,532 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2016-12-25 22:27:21,532 DEBUG [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed

更新: 我在服务中添加了另一个用于执行读取操作的方法及其在实现类中的相应逻辑。现在我能够以 xml 和 json 格式获得响应。我添加了以下方法

    @WebMethod
@GET
@Path("{location}")
@Descriptions({
    @Description(value = "returns a location data ", target = DocTarget.METHOD),
    @Description(value = "the location data", target = DocTarget.RETURN)
})
public LocationData readLocation(@Description(value = "the string representation of the location") @PathParam("location") @NotNull @Size(max=10, min=5) String location) throws LocationNotFoundException;

但是我想在自己执行创建操作时以 json 和 xml 格式响应

输出:Instead of Delhi, i will be having location, when i click on URL i get blank page

我将拥有“位置”而不是德里,当我点击 URL 时我得到空白页

最佳答案

(评论中的解决方案)

潜在的问题是需要使用 URL 链接调用 JAX-RS 方法

@POST
@Path("location")
@Produces({"application/xml","application/json"})
public LocationData createLocation(@Valid LocationData locationData) throws DuplicateLocationException;

无法发送带有 JSON 点击 URL 的 POST 请求。 点击链接或在浏览器工具栏中引入 URL 会执行 GET 请求。

第二种方法之所以有效,是因为使用了 GET 和 URL 路径 /rest/location/{location}。该方法不能接收JSON数据,但可以返回JSON。

您必须使用 AJAX,通过 POST 发送 json 或 XML 来调用 REST 服务。或者,您可以使用查询参数发布点击 URL 中的数据

rest/location/create?location=Delhi&timezone= 

但这不是很正统

关于java - 使用 apache cxf 的 Rest websevice 响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319830/

相关文章:

java - @PropertyKey 注解不适用于 Kotlin,但适用于 Java

java - 如何通过 javax.xml.ws.Service 进行调用

java - 如何使用 jersey 在 Java IDE 中完全模拟客户端-服务器 Restful Web 服务,而无需部署到 Web 服务器?

django - 在 Django 中使用 REST API 框架而不是简单的 URL 和 View 创建的优势?

java - 来自 url 的 http 或 https

java - 获取 wso2 esb 中内存消息存储中的消息数

c# - Windows 10 IoT - REST API 部署

xml - 使用 XPath 在格式不佳的 XML 中查找 XML 同级

java - 如何使用Java在SQL查询中指定数据库

java - WS 到服务关注点