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?)