I have the following classes: ServerStartup.java
import java.io.IOException;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.net.httpserver.HttpServer;
public class ServerStartUp {
static final String BASE_URI = "http://localhost:9999/";
public static void main(String[] args) {
try {
final ResourceConfig rc = new PackagesResourceConfig("com.twins.engine");
rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
HttpServer server = HttpServerFactory.create(BASE_URI,rc);
server.setExecutor(null);
server.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and the following class SysInfo.Java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.twins.engine.model.SysInfo;
@Path("/Sys")
public class General {
@GET
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)
public SysInfo getInfo(){
SysInfo info = new SysInfo("Monetoring 365");
return info;
}
}
and the following model class SysInfo.java
public class SysInfo {
private String name;
public SysInfo(String name) {
this.setName(name);
}
public String getName() {return name;}
public void setName(String name) {this.name = name; }
}
When i run the serverStartup class and Navigate to the following url: http://127.0.0.1:9999/sys/info
i got the following exception:
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.twins.engine.model.SysInfo, and Java type class com.twins.engine.model.SysInfo, and MIME media type text/html was not found ... 15 more
enter image description here as well i have the following Jars attached as image, is there any missing configuration i have to do
Try following
final ResourceConfig rc = new PackagesResourceConfig("com.twins.engine");
final Map<String, Object> config = new HashMap<String, Object>();
config.put("com.sun.jersey.api.json.POJOMappingFeature", true);
rc.setPropertiesAndFeatures(config);
HttpServer server = HttpServerFactory.create(BASE_URI,rc);
server.setExecutor(null);
server.start();
and probably you have to add jackson-mapper
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.12</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.12</version> </dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.