List of usage examples for org.springframework.http MediaType MediaType
public MediaType(String type, String subtype, @Nullable Map<String, String> parameters)
From source file:org.ng200.openolympus.controller.api.TaskDescriptionSourcecodeController.java
@RequestMapping(value = "/api/taskSourcecode", method = RequestMethod.GET) public @ResponseBody ResponseEntity<String> getTaskSourcecode(@RequestParam(value = "id") final Task task) throws IOException { Assertions.resourceExists(task);//from w w w . j av a 2s .c o m final String descriptionPath = MessageFormat.format(TaskUploader.TASK_DESCRIPTION_PATH_TEMPLATE, StorageSpace.STORAGE_PREFIX, task.getTaskLocation()); final File descriptionFile = new File(descriptionPath); HttpHeaders responseHeaders = new HttpHeaders(); Charset charset = Charset.forName("UTF-8"); responseHeaders.setContentType(new MediaType("text", "plain", charset)); return new ResponseEntity<String>(new String(FileAccess.readAllBytes(descriptionFile), charset), responseHeaders, HttpStatus.OK); }
From source file:controllers.EventTypeControllerTest.java
/** * Test of getEventTypesByTransport method, of class EventTypeController. */// w w w.j a v a2 s.c om @Test public void testGetEventTypesByTransport() throws DataAccessException, RecordNotFoundException, Exception { EventType eventType1 = new EventType("Jam", Arrays.asList(Transportation.CAR)); EventType eventType2 = new EventType("RoadWork", Arrays.asList(Transportation.CAR)); when(databaseMock.getEventtypes(Transportation.CAR)) .thenReturn(Arrays.asList(new Pair(1, eventType1), new Pair(1, eventType2))); mockMVC.perform(get("/eventtype/").param("transportationType", "car").characterEncoding("UTF-8")) .andExpect(status().isOk()) .andExpect(content().contentType(new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")))) .andExpect(jsonPath("$", hasSize(2))).andExpect(jsonPath("$[0].type", is("Jam"))) .andExpect(jsonPath("$[1].type", is("RoadWork"))); }
From source file:net.eusashead.hateoas.conditional.interceptor.SyncTestControllerITCase.java
@Test public void testConditionalGet() throws Exception { this.mockMvc//from w w w.java2 s .c om .perform(get("http://localhost/sync/123").contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andExpect(header().string("ETag", "\"123456\"")) .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8")))) .andReturn(); this.mockMvc .perform(get("http://localhost/sync/123").contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON).header("If-None-Match", "\"123456\"")) .andExpect(status().isNotModified()).andExpect(content().string("")).andReturn(); }
From source file:com.wq.common.web.springmvc.MappingJackson2HttpMessageConverter.java
/** * Construct a new {@code BindingJacksonHttpMessageConverter}. *///from w w w . j av a 2s . co m public MappingJackson2HttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET)); objectMapper.setDateFormat(new SimpleDateFormat(JsonDateFormat.formatString)); }
From source file:org.resthub.web.converter.MappingJackson2XmlHttpMessageConverter.java
/** * Construct a new {@code BindingJacksonHttpMessageConverter}. *//*ww w . jav a 2 s .co m*/ public MappingJackson2XmlHttpMessageConverter() { super(new MediaType("application", "xml", DEFAULT_CHARSET)); JacksonXmlModule xmlModule = new JacksonXmlModule(); xmlModule.setDefaultUseWrapper(false); objectMapper = new XmlMapper(xmlModule); SimpleModule module = new SimpleModule(); module.addAbstractTypeMapping(Page.class, PageResponse.class); objectMapper.registerModule(module); AnnotationIntrospector introspector = new JacksonAnnotationIntrospector(); objectMapper.setAnnotationIntrospector(introspector); }
From source file:net.eusashead.hateoas.response.argumentresolver.ResponseBuilderMethodArgumentResolverITCase.java
@Test public void testGet() throws Exception { this.mockMvc/* ww w. j a v a2s. c o m*/ .perform(get("http://localhost/entity").contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(header().string("ETag", "W/\"b0c689597eb9dd62c0a1fb90a7c5c5a5\"")) .andExpect(content().contentType(new MediaType("application", "json", Charset.forName("UTF-8")))) .andReturn(); }
From source file:net.eusashead.hateoas.hal.response.impl.HalResponseBuilderITCase.java
@Test public void testGet() throws Exception { // Execute controller method MvcResult result = this.mockMvc .perform(get("http://localhost/order/123").contentType(HalHttpMessageConverter.HAL_JSON) .accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isOk()).andExpect(header().string("ETag", "W/\"123456789\"")) .andExpect(//from w ww. jav a 2 s.c om content().contentType(new MediaType("application", "hal+json", Charset.forName("UTF-8")))) .andReturn(); // Check result ReadableRepresentation returned = new StandardRepresentationFactory() .readRepresentation(new StringReader(result.getResponse().getContentAsString())); Assert.assertNotNull(returned); Link linkByRel = returned.getLinkByRel("self"); Assert.assertNotNull(linkByRel); Assert.assertEquals("/order/123", linkByRel.getHref()); Object total = returned.getValue("total"); Assert.assertEquals(BigDecimal.valueOf(12.34d).toString(), total); }
From source file:org.resthub.web.converter.MappingJackson2JsonHttpMessageConverter.java
/** * Construct a new {@code BindingJacksonHttpMessageConverter}. *///from w ww .ja va 2s. c o m public MappingJackson2JsonHttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET)); objectMapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addAbstractTypeMapping(Page.class, PageResponse.class); objectMapper.registerModule(module); AnnotationIntrospector introspector = new JacksonAnnotationIntrospector(); objectMapper.setAnnotationIntrospector(introspector); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
From source file:net.eusashead.hateoas.response.impl.HalPageResponseBuilderITCase.java
@Test public void testGet() throws Exception { // Execute controller method MvcResult result = this.mockMvc .perform(get("http://localhost/customer/?page=1&size=15") .contentType(HalHttpMessageConverter.HAL_JSON).accept(HalHttpMessageConverter.HAL_JSON)) .andExpect(status().isOk()).andExpect(header().string("ETag", "W/\"123456789\"")) .andExpect(// w w w . j a v a 2 s.c o m content().contentType(new MediaType("application", "hal+json", Charset.forName("UTF-8")))) .andReturn(); // Check result ReadableRepresentation returned = new StandardRepresentationFactory() .readRepresentation(new StringReader(result.getResponse().getContentAsString())); Assert.assertNotNull(returned); // Self link Link self = returned.getLinkByRel("self"); Assert.assertNotNull(self); Assert.assertEquals("/customer/?page=1&size=15", self.getHref()); // Self link Link next = returned.getLinkByRel("next"); Assert.assertNotNull(next); Assert.assertEquals("/customer/?page=2&size=15", next.getHref()); // Previous link Link previous = returned.getLinkByRel("previous"); Assert.assertNotNull(previous); Assert.assertEquals("/customer/?page=0&size=15", previous.getHref()); // Pagination parameters Object size = returned.getValue("size"); Assert.assertEquals(Integer.valueOf(15).toString(), size); Object page = returned.getValue("page"); Assert.assertEquals(Integer.valueOf(1).toString(), page); Object numberOfElements = returned.getValue("numberOfElements"); Assert.assertEquals(Integer.valueOf(15).toString(), numberOfElements); Object totalElements = returned.getValue("totalElements"); Assert.assertEquals(Integer.valueOf(95).toString(), totalElements); // Check the body List<? extends ReadableRepresentation> content = returned.getResourcesByRel("content"); Assert.assertNotNull(content); Assert.assertEquals(Integer.valueOf(15), Integer.valueOf(content.size())); }
From source file:org.appverse.web.framework.backend.frontfacade.json.controllers.CustomMappingJacksonHttpMessageConverter.java
/** * Construct a new {@code BindingJacksonHttpMessageConverter}. *///from w ww. jav a 2s. co m public CustomMappingJacksonHttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET)); }