我无法在Resteasy中注入cdi bean.在调试时,它似乎总是显示空指针异常.即以下代码中的'jaxRsImpl'始终为null.我试图在jboss eap 6.2上运行
@Path("/jaxrs-service")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JAXRSService {
@Inject
private JAXRSImpl jaxRsImpl;
@POST
@Path("/authenticate")
@Consumes(MediaType.APPLICATION_JSON)
public Response authenticate(Credentials user) {
return jaxRsImpl.authenticate(user);
}
}
Run Code Online (Sandbox Code Playgroud)
而我打算注入的课程是
@RequestScoped
public class JAXRSImpl {
public Response authenticate(Credentials user) {
// Some logic
}
}
Run Code Online (Sandbox Code Playgroud)
由于我的应用程序是web,所以我在WEB-INF文件夹中添加了beans.xml
我的Initialiser看起来像
@ApplicationPath("/rest")
public class JAXRSInitializer extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public JAXRSInitializer() {
singletons.add(new JAXRSService());
classes.add(JAXRSImpl.class);
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override …Run Code Online (Sandbox Code Playgroud) 早上好.今天早上,当我通过泽西实体提供商MessageBodyReader和MessageBodyWriters时,我遇到了以下问题.
我想编写一个资源方法和客户端,它返回一个自定义对象列表和媒体类型application/xml.所以我想使用JAXB(我是JAXB的新手).我能够通过编写自己的扩展MessageBodyReader和来实现这一目标MessageBodyWriter.但我害怕我跟随的方式.看看我实施的方式:
资源方法:
@Path("productlist/xml")
@GET
public RetObjects getProductsXml(){
List<Product> pList = new ArrayList<Product>();
pList.add(new Product("1","Dell latitude E6000",2900,500));
pList.add(new Product("2","Xperia Z2",549,400));
RetObjects obj = new RetObjects();
obj.setObject(pList);
return obj;
}
Run Code Online (Sandbox Code Playgroud)
我的自定义对象:
@Entity
@Table (name="PRODUCT")
@XmlRootElement(name="product")
public class Product {
@Id
@Column(name = "CODE")
private String code;
...
// rest of the fields, constructors, getters and setters
}
Run Code Online (Sandbox Code Playgroud)
包装我的自定义对象列表的对象:
@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class RetObjects {
@XmlElement(name = "product")
private …Run Code Online (Sandbox Code Playgroud) 我想使用非spring bean类对象作为jersey web服务类方法的参数.但它在构建时给出了缺少的依赖性错误.
我的代码是:
@Component
@Path("/abcd")
public class ActorServiceEndpoint {
@POST
@Path("/test/{nonspringBean}")
@Produces(MediaType.APPLICATION_XML)
public void addActor(@PathParam("nonspringBean") MyNonSpringBeanClass nonspringBean){
}
}
Run Code Online (Sandbox Code Playgroud) 我似乎无法让这个为我工作,我已经在其他帖子中看到这一点,并希望有人可能能够发现我做错了什么.我正试图得到这个休息api的请求的身体但似乎无法拉回我需要的东西,然后在下面的字符串中得到null.
@POST
@Path("/SetFeeds")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String setFeed(@PathParam("name")String name2, @QueryParam("name") String name,@Context UriInfo uriInfo){
MultivaluedMap<String,String> queryParams = uriInfo.getQueryParameters();
String query = uriInfo.getRequestUri().getQuery();
String response = queryParams.getFirst("name");
return response;
}
Run Code Online (Sandbox Code Playgroud) 在RESTful API中,很典型的做法是查看可以支持多种序列化格式的端点:
// Sends back "fizz" resource that has an id=34 as a JSON object
http://api.example.com/v2/fizz/34.json
// Sends back "fizz" resource that has an id=34 as an XML object
http://api.example.com/v2/fizz/34.xml
// Sends back "fizz" resource that has an id=34 as a binary object,
// say, using Google Protocol Buffers
http://api.example.com/v2/fizz/34.bin
Run Code Online (Sandbox Code Playgroud)
我正在设计一个Dropwizard服务,并试图弄清楚如何实现多种格式支持,但是在这方面文档很贫乏。有任何想法吗?
我正在尝试创建一个自定义约束验证器注释。这是我下面的注释定义。Eclipse抱怨“此位置不允许使用目标注释”。保留和约束也是如此。我正在使用Java 1.7
package com.test;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target(PARAMETER)
@Retention(RUNTIME)
@Constraint(validatedBy = MyValidator.class)
public interface MyValidationAnnotation{
String message() ;
Class<?>[] groups() ;
Class<? extends Payload>[] payload() ;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将jax-rs响应序列化为json字符串.
来自服务器的响应是json,我从泽西客户端获得它:
Response resp = target.request().method("PUT", Entity.json(payloadBean))
Run Code Online (Sandbox Code Playgroud)
其中payloadBean是我的json请求.一切正常但我无法转换json字符串中的resp以便记录它.
如果我尝试:
String s = EntityUtils.toString((HttpEntity) resp.getEntity());
Run Code Online (Sandbox Code Playgroud)
我明白了:
org.glassfish.jersey.client.internal.HttpUrlConnector cannot be cast to org.apache.http.HttpEntity
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果我没有强制转换为HttpEntity,编译器说:
toString (org.apache.http.HttpEntity) in EntityUtils cannot be applied to (java.lang.Object).
Run Code Online (Sandbox Code Playgroud)
我的相关进口是:
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在尝试通过遵循此https://jersey.java.net/documentation/latest/test-framework.html来测试Jax-rs资源,
并且我正在使用容器jersey-test-framework-provider-jdk-http
我可以声明状态码。但是,当我尝试读取实体时,出现异常:
javax.ws.rs.ProcessingException: Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String
at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:39)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:248)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:217)
Run Code Online (Sandbox Code Playgroud)
我的资源分类:
@Path("/")
public class SampleResource {
@GET
@Path("/health")
@Produces(MediaType.APPLICATION_JSON)
public String getServiceStatus() {
return "{\"Status\": \"OK\"}";
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试班:
public class TestSampleResource extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(SampleResource.class);
}
@Test
public void testHealthEndpoint() {
Response healthResponse = target("health").request(MediaType.APPLICATION_JSON).get();
Assert.assertEquals(200, healthResponse.getstatus()); // works
String body …Run Code Online (Sandbox Code Playgroud) 我正在尝试我的第一个REST应用程序.
以下是我的配置:
以下是Maven下载的罐子列表:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.prasad</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
<exclusions>
</exclusions>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency> …Run Code Online (Sandbox Code Playgroud) 当我在Tomcat 8上使用JDK 8从Eclipse运行我的Web项目时,一切都运行良好,但是一旦我构建了这个项目并将WAR部署到服务器上的Tomcat 8,我就会收到以下错误:
java.lang.IncompatibleClassChangeError: com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider and com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$Wadl disagree on InnerClasses attribute
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一切,但它仍然无法正常工作.
这是我的pom.xml档案:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RestWebService</groupId>
<artifactId>RestWebService</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId> …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×7
jersey ×6
rest ×4
jboss7.x ×2
jersey-2.0 ×2
resteasy ×2
cdi ×1
dropwizard ×1
eclipse ×1
javax.ws.rs ×1
jaxb ×1
junit ×1
maven ×1
tomcat8 ×1
web-services ×1