Build RESTful Service 3 with Jersey-convert JAVA object to JSON output

Source: Internet
Author: User
Tags glassfish

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.