English 中文(简体)
• 如何在日本宇宙航空研究开发机构恢复性网络服务中接收日本宇宙航空研究学会的短信?
原标题:How to receive JSON Messages in POST body in a JAX-RS Restful web service in CXF?

I m试图利用阿帕奇-CXF在日本宇宙航空研究开发机构-RS的前线开发一个REE服务。 对于开端人,我有一个称为<条码>测试的方法,该方法接收<条码>、条码信息和<条码>。 我希望客户能够把这些参数输入到一个局域网信息机构。 我似乎无法做到这一点。

在我之前,这里有一些细节:

  • I m using CXF without Spring
  • It s not a web app, so I don t have the WEB-INF folder with the web.xml
  • I test the service using SoapUI and Postman (Google Chrome application)

我收到以下代码<代码>。 WARNING: javax.ws.rs.BadRequestException: HTTP 400 Bad Request:

DemoService.java

@WebService(targetNamespace = "http://demoservice.com")
@Path("/demoService")
public interface DemoService {
    @POST
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String test (String message, int value);
}

DemoserviceImpl.java

public class DemoServiceImpl implements DemoService {
    @Override
    public String test(String message, int value) {
        return "test message: " + message + " value =  : " + value;
    }
}

DemoServer.java

public class DemoServer{
    public static void main(String[] args) {

        JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
        DemoService demoService = new DemoServiceImpl();
        serverFactory.setServiceBean(demoService);
        serverFactory.setAddress("http://localhost:9090");
        serverFactory.create();
    }
}

My POM.xml (minus the characteristics in the fundamental tag, all s there)

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>demo</groupId>
    <artifactId>demoService</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <cxf.version>3.0.0</cxf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <!-- Jetty is needed if you re are not using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-service-description</artifactId>
            <version>3.0.0-milestone1</version>
        </dependency>

    </dependencies>
</project>

Testing with {"message":"hello there!", "value":"50"} to the URL http://localhost:9090/demoService/test gave a HTTP 400 Bad Reuest.

Then I saw this question on S.O.: How to access parameters in a RESTful POST method and tried this:

添加了DemoServer的以下nes类。 java:

    @XmlRootElement
    public static class TestRequest {
        private String message;
        private int value;

        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
        public int getValue() { return value; }
        public void setValue(int value) { this.value = value; }
    }

我还修改了“民主服务”接口和执行,将这一类别作为<条码>测试<>/代码>方法的参数,尽管这最终仍不是我想要做的事。 (在此说明执行情况,问题已久)

@Override
public String test(TestRequest testRequest) {
    String message = testRequest.getMessage();
    int value = testRequest.getValue();
    return "test message: " + message + " value =  : " + value;
}

And to fix this error that I got: SEVERE: No message body reader has been found for class DemoService$TestRequest, ContentType: application/json (in Postman I see error 415 - unsupported media type) I added the following dependencies (jettison and another thing) to the POM.xml:

<dependency>
    <groupId>org.codehaus.jettison</groupId>
    <artifactId>jettison</artifactId>
    <version>1.3.5</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-extension-providers</artifactId>
    <version>2.6.0</version>
</dependency>

我在一份吉斯蒂尔·波萨雷斯的请求中,用以下信对这项服务进行了测试:

{"testRequest":{"message":"hello there!", "value":"50"}}

这一工作。 尽管这一解决办法是,我使用测试研究组来概括参数的工作,但这不是解决办法一。 我想通过JSON电文中的两个参数,而不必引入这一试卷(明示)。

问题:

  • Would this be easier to implement using Jersey?
  • I don t have a web.xml nor a WEB-INF folder, so I can t configure CXF in a cxf.xml file can I? A lot of tutorials online seem ot use a lot of XML configuration, but I don t want to deploy a framework like TomEE or Spring or Glassfish just to do that.
  • Searching online for solutions, I came across Spring Boot. Would you recommend using that, perhaps? Would that make developing web services like this easier?
  • Also, how do I get it to return the value in JSON format (or is it not supposed to do that for Strings?)
问题回答

我的朋友向我指出了这一分点交换问题:JAX-RS 多个目标

and also the following documentation: http://cxf.apache.org/docs/jax-rs-and-jax-ws.html

which states:

public class CustomerService { public void doIt(String a, String b) {...}; }

By default JAX-RS may not be able to handle such methods as it requires that only a single parameter can be available in a signature that is not annotated by one of the JAX-RS annotations like @PathParam. So if a String a parameter can be mapped to a @Path template variable or one of the query segments then this signature won t need to be changed :

@Path("/customers/{a}") public class CustomerService { public void doIt(@PathParam("a") String a, String b) {...}; }

因此,为了回答我的问题,诺先生无法回答。





相关问题
IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

ASP.net web services

I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/...

Unity Container Disposing and XML Web Service

I am registering some wrapers over un-managed objects in container. How can I dispose of them at the end of the container s lifetime? Please bear in mind I have an XML Web service.

SharePoint : web service permission error

I have a sharepoint site, and I am calling a standard sharepoint web service. I create the web service request like this : wsDws.Url = this.SiteAddress + @"/_vti_bin/Dws.asmx"; When I use ...

热门标签