I. General description
XML and JSON are the most common data exchange formats. This example shows how to convert a java object to JSON output.
Ii. Process
1. In the project above,
Add the code in "com. waylau. rest. resources. UserResource". The Code is as follows:
@GET @Path("/getUserJson") @Produces(MediaType.APPLICATION_JSON) public User getUserJson() { User user = new User(); user.setAge("27"); user.setUserId("005"); user.setUserName("Fmand"); return user; }
MediaType. APPLICATION_JSON indicates that the output is in JSON format.
2. Run the project and enter http: // localhost: 8089/RestDemo/rest/users/getUserJson in the browser.
Expected to obtain json data. At this time, the project reports an error.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.waylau.rest.bean.User, genericType=class com.waylau.rest.bean.User.at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)at org.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:293)at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)
In this case, you need to obtain support for json conversion packages. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + v8nS1NPJtuDW1re9yr3Ktc/expires + expires/1tDS/expires + CjxwPjxwcmUgY2xhc3M9 "brush: java;"> package com. waylau. rest; import org. codehaus. jackson. jaxrs. jacksonJsonProvider; import org. glassfish. jersey. filter. loggingFilter; import org. glassfish. jersey. server. resourceConfig;/*** application * @ author waylau.com * 2014-3-18 */public class RestApplication extends ResourceConfig {public RestApplication () {// package path of the service class package ("com. waylau. rest. resources "); // register the JSON converter register (JacksonJsonProvider. class );}}
6. modify web. xml and initialize the application from RestApplicaton, as shown below:
Way REST Service
org.glassfish.jersey.servlet.ServletContainer
javax.ws.rs.Application
com.waylau.rest.RestApplication
1
Way REST Service
/rest/*
7. Run the project and access http: // localhost: 8089/RestDemo/rest/users/getUserJson again.
JSON text output