Search in sources :

Example 26 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project trainning by fernandotomasio.

the class TesteMoodle4HTTP method main.

public static void main(String[] args) throws UnsupportedEncodingException, MalformedURLException, ProtocolException, IOException {
    String token = ConfigHelper.getValue("moodle.token");
    String domainName = ConfigHelper.getValue("moodle.domain");
    // Also possible in Moodle 2.2 and later: 'json'
    String restformat = "jsoSn";
    // Setting it to 'json' will fail all calls on earlier Moodle version
    if (restformat.equals("json")) {
        restformat = "&moodlewsrestformat=" + restformat;
    } else {
        restformat = "";
    }
    String postFunctionName = "core_course_create_categories";
    String serverurl = domainName + "/webservice/rest/server.php" + "?wstoken=" + token + "&wsfunction=" + postFunctionName + restformat;
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
    list.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(list);
    // String urlParameters
    // = "categories[0][name]=" + URLEncoder.encode("testusername1", "UTF-8")
    // + "&categories[0][parent]=" + URLEncoder.encode("1", "UTF-8")
    // + "&categories[0][idnumber]=" + URLEncoder.encode("", "UTF-8")
    // + "&categories[0][description]=" + URLEncoder.encode("", "UTF-8")
    // + "&categories[0][descriptionformat]=" + URLEncoder.encode("1", "UTF-8");
    // 
    // 
    // HttpURLConnection con = (HttpURLConnection) new URL(serverurl).openConnection();
    // con.setRequestMethod("POST");
    // con.setDoOutput(true);
    // con.setUseCaches(false);
    // con.setDoInput(true);
    // DataOutputStream wr = new DataOutputStream(
    // con.getOutputStream());
    // wr.writeBytes(urlParameters);
    // wr.flush();
    // wr.close();
    // 
    // InputStream is = con.getInputStream();
    // BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    // String line;
    // StringBuilder response = new StringBuilder();
    // while ((line = rd.readLine()) != null) {
    // response.append(line);
    // response.append('\r');
    // }
    // rd.close();
    // System.out.println(response.toString());
    Category category = new Category();
    category.setName("teste8");
    category.setParent(1);
    category.setDescriptionformat(1);
    Category[] categories = new Category[1];
    categories[0] = category;
    restTemplate.postForLocation(serverurl, categories);
// System.out.println(courses.length);
// 
// MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
// map.add("fullname", "sadfasdfasdf");
// map.add("shortname", "asdfasdfasdfasdf");
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// 
// HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
// 
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
// messageConverters.add(new MappingJackson2HttpMessageConverter());
// messageConverters.add(new FormHttpMessageConverter());
// restTemplate.setMessageConverters(messageConverters);
// Course response = restTemplate.postForObject(serverurl, request, Course.class);
// 
// System.out.println(response.getFullname());
// Course c = new Course();
// c.setFullname("alskjdfçasdf");
// c.setShortname("ldslkdf");
// c.setCategoryid(1);
// List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
// list.add(new MappingJackson2HttpMessageConverter());
// restTemplate.setMessageConverters(list);
// Course course = restTemplate.postForObject(serverurl, c, Course.class);
// System.out.println(course.getFullname());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Category(com.tomasio.projects.trainning.jackson.Category) RestTemplate(org.springframework.web.client.RestTemplate) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList)

Example 27 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project trainning by fernandotomasio.

the class TesteMoodle2 method main.

public static void main(String[] args) {
    String token = ConfigHelper.getValue("moodle.token");
    String domainName = ConfigHelper.getValue("moodle.domain");
    // Also possible in Moodle 2.2 and later: 'json'
    String restformat = "json";
    // Setting it to 'json' will fail all calls on earlier Moodle version
    if (restformat.equals("json")) {
        restformat = "&moodlewsrestformat=" + restformat;
    } else {
        restformat = "";
    }
    String postFunctionName = "core_course_create_courses";
    String serverurl = domainName + "/webservice/rest/server.php" + "?wstoken=" + token + "&wsfunction=" + postFunctionName + restformat;
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
    list.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(list);
    Course myCourse = new Course();
    myCourse.setFullname("my course");
    myCourse.setShortname("MC");
    myCourse.setId(5);
    restTemplate.postForObject(serverurl, myCourse, Object.class);
// System.out.println(courses.length);
// 
// MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
// map.add("fullname", "sadfasdfasdf");
// map.add("shortname", "asdfasdfasdfasdf");
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// 
// HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
// 
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
// messageConverters.add(new MappingJackson2HttpMessageConverter());
// messageConverters.add(new FormHttpMessageConverter());
// restTemplate.setMessageConverters(messageConverters);
// Course response = restTemplate.postForObject(serverurl, request, Course.class);
// 
// System.out.println(response.getFullname());
// Course c = new Course();
// c.setFullname("alskjdfçasdf");
// c.setShortname("ldslkdf");
// c.setCategoryid(1);
// List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
// list.add(new MappingJackson2HttpMessageConverter());
// restTemplate.setMessageConverters(list);
// Course course = restTemplate.postForObject(serverurl, c, Course.class);
// System.out.println(course.getFullname());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) Course(com.tomasio.projects.trainning.jackson.Course)

Example 28 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project ma-modules-public by infiniteautomation.

the class BaseRestTest method setupMvc.

/**
 * @param controllers
 */
protected void setupMvc(Object... controllers) {
    // Mock our Daos so they
    // return exactly what we want.
    // TODO
    // DaoRegistry.dataPointDao = this.dataPointDao;
    // DaoRegistry.dataSourceDao = this.dataSourceDao;
    // DaoRegistry.userDao = this.userDao;
    // DaoRegistry.pointValueDao = this.pointValueDao;
    this.objectMapper = MangoRestSpringConfiguration.createNewObjectMapper();
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(this.objectMapper);
    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(converter);
    this.mockMvc = standaloneSetup(controllers).setMessageConverters(converter).setHandlerExceptionResolvers(createHandlerExceptionResolvers(converters)).build();
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList)

Example 29 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project profile by craftercms.

the class AbstractRestHandlerTestBase method createResponseWriter.

protected HttpMessageConvertingResponseWriter createResponseWriter() {
    ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
    factoryBean.afterPropertiesSet();
    ContentNegotiationManager contentNegotiationManager = factoryBean.getObject();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    List<JsonSerializer<?>> serializers = new ArrayList<>();
    serializers.add(new ObjectIdSerializer());
    Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<>();
    deserializers.put(ObjectId.class, new ObjectIdDeserializer());
    CustomSerializationObjectMapper objectMapper = new CustomSerializationObjectMapper();
    objectMapper.setSerializers(serializers);
    objectMapper.setDeserializers(deserializers);
    objectMapper.init();
    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(objectMapper);
    messageConverters.add(jsonMessageConverter);
    return new HttpMessageConvertingResponseWriter(contentNegotiationManager, messageConverters);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) HttpMessageConvertingResponseWriter(org.craftercms.commons.rest.HttpMessageConvertingResponseWriter) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ObjectIdDeserializer(org.craftercms.commons.jackson.ObjectIdDeserializer) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) CustomSerializationObjectMapper(org.craftercms.commons.jackson.CustomSerializationObjectMapper) ObjectIdSerializer(org.craftercms.commons.jackson.ObjectIdSerializer)

Example 30 with MappingJackson2HttpMessageConverter

use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project osms by malikov-yurii.

the class SpringMvc method converter.

@Bean
public MappingJackson2HttpMessageConverter converter() {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(objectMapper());
    return converter;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Aggregations

MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)122 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)51 ArrayList (java.util.ArrayList)46 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)38 Test (org.junit.jupiter.api.Test)28 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)25 RestTemplate (org.springframework.web.client.RestTemplate)25 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)19 HandlerMethod (org.springframework.web.method.HandlerMethod)17 Method (java.lang.reflect.Method)16 Test (org.junit.Test)16 MethodParameter (org.springframework.core.MethodParameter)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bean (org.springframework.context.annotation.Bean)14 List (java.util.List)9 HttpEntity (org.springframework.http.HttpEntity)7 MediaType (org.springframework.http.MediaType)6 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)6