Created
October 23, 2012 19:04
-
-
Save dwelch2344/3940884 to your computer and use it in GitHub Desktop.
A untested JsonService for Andre
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import javax.inject.Inject; | |
| import org.springframework.http.HttpHeaders; | |
| import org.springframework.http.HttpOutputMessage; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; | |
| import org.springframework.stereotype.Service; | |
| @Service | |
| public class JsonServiceImpl { | |
| @Inject | |
| private MappingJacksonHttpMessageConverter converter; | |
| public String toJson(Object o) { | |
| MediaType contentType = new MediaType("application", "json"); | |
| HttpOutputMessage outputMessage = new HttpOutputMessage() { | |
| private ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| @Override | |
| public HttpHeaders getHeaders() { | |
| throw new UnsupportedOperationException("Not implemented"); | |
| } | |
| @Override | |
| public OutputStream getBody() throws IOException { | |
| return baos; | |
| } | |
| @Override | |
| public String toString() { | |
| return baos.toString(); | |
| } | |
| }; | |
| try { | |
| converter.write(o, contentType, outputMessage); | |
| String val = outputMessage.toString(); | |
| return val; | |
| // return objectMapper.writeValueAsString(o); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| return "{\"error\":\"object mapping exception\"}"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment