SyntaxHighlighter

Showing posts with label REST. Show all posts
Showing posts with label REST. Show all posts

Thursday, October 26, 2017

Java to .Net - REST APIs and Controllers - Some notes

some quick notes about using .NET Core controllers in C#;
- the url maps to the name of the controller class

so, class BlahController : Controller will create a URL of /blah


Wednesday, April 17, 2013

Spring WebMVC - returning XML instead of JSON using XStream

Spring WebMVC - returning XML instead of JSON using XStream

here's a little set of code i put together in response to a stackoverflow question (http://stackoverflow.com/questions/16060973/configuring-spring-mvc-rest-to-return-only-xml/160621420)

here's the entity that i'm going to expose

package de.incompleteco.spring.domain;

import java.io.Serializable;

public class SimpleEntity implements Serializable {

 private Long id;
 
 private String stuff;
 
 public SimpleEntity() {
  this.id = System.currentTimeMillis();
 }

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getStuff() {
  return stuff;
 }

 public void setStuff(String stuff) {
  this.stuff = stuff;
 }
 
 
 
}


here's the very simple controller

package de.incompleteco.spring.web.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.incompleteco.spring.domain.SimpleEntity;

@Controller
@RequestMapping("/simpleEntity")
public class SimpleRestController {

 
 @RequestMapping(method=RequestMethod.GET,produces=MediaType.APPLICATION_XML_VALUE)
 public @ResponseBody SimpleEntity get() {
  return new SimpleEntity();
 }
 
}



here's the xml way of wiring it up to use XStream as the XML Streaming engine


 
  
   
    
    
    
     
      #{T(org.springframework.http.MediaType).APPLICATION_XML_VALUE}
     
    
   
  
 
 
 

 





here's the @Configuration way to do the same

package de.incompleteco.spring.web;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.support.AbstractMarshaller;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@ComponentScan("de.incompleteco.spring.web")
public class SimpleConfiguration extends WebMvcConfigurerAdapter {
 
 private XStreamMarshaller marshaller;
 
 @Override
 public void configureMessageConverters(List> converters) {
  converters.add(getHttpMessageConverter());
  super.configureMessageConverters(converters);
  
 }

 public HttpMessageConverter getHttpMessageConverter() {
  MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
  converter.setMarshaller(marshaller());
  converter.setUnmarshaller(marshaller());
  return converter;
 }

 @Bean
 public AbstractMarshaller marshaller() {
  if (marshaller == null) {
   return new XStreamMarshaller();
  } else {
   return marshaller;
  }
 }
 
}



and here's the tests for the code.
package de.incompleteco.spring.web.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:/META-INF/spring/servlet-context.xml"})
public class SimpleRestControllerTest {

 @Resource
 private WebApplicationContext context;
 
 @Test
 public void testGet() throws Exception {
  MockMvc mvc = webAppContextSetup(context).build();
  //test
  MvcResult result = mvc.perform(get("/simpleEntity").contentType(MediaType.TEXT_HTML)).andExpect(status().isOk()).andReturn();
  //now show the context
  System.out.println(result.getResponse().getContentAsString());
 }

}



and
package de.incompleteco.spring.web.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;

import de.incompleteco.spring.web.SimpleConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes=SimpleConfiguration.class)
public class ConfigSimpleRestControllerTest {

 @Resource
 private WebApplicationContext context;
 
 @Test
 public void testGet() throws Exception {
  MockMvc mvc = webAppContextSetup(context).build();
  //test
  MvcResult result = mvc.perform(get("/simpleEntity").contentType(MediaType.TEXT_HTML)).andExpect(status().isOk()).andReturn();
  //now show the context
  System.out.println(result.getResponse().getContentAsString());
 }

}


one thing to note: you'll need to comment out/keep the @Configuration separate from the XML - the XML scans and hits the @Configuration annotation and tries to an appcontext inside and appcontext (and dies)

and here's the pom too...

  4.0.0
  de.incompleteco.spring.webmvc
  spring-webmvc-rest-xml
  0.0.1-SNAPSHOT
  war
  spring-webmvc-rest-xml
  
   
     
       org.apache.maven.plugins
       maven-compiler-plugin
       
         1.7
         1.7
       
     
      
    
  
   
    org.springframework
    spring-webmvc
    3.2.1.RELEASE
   
   
    org.springframework
    spring-test
    3.2.1.RELEASE
    test
   
   
    junit
    junit
    4.10
    test
   
   
    javax.servlet
    javax.servlet-api
    3.0.1
    provided
   
   
    org.springframework
    spring-web
    3.2.1.RELEASE
   
   
    log4j
    log4j
    1.2.17
    test
   
   
    org.springframework
    spring-oxm
    3.2.1.RELEASE
   
   
    com.thoughtworks.xstream
    xstream
    1.4.4
   
  

Monday, April 15, 2013

Friday, April 5, 2013

Spring Integration JPA and REST

Spring Integration JPA and REST


here's a little sample project i put together to illustrate how to expose JPA objects via REST using Spring Integration.  it's a bit rough but it does the following;
- PUT a new entity
- GET an entity by id
- GET all entities

here's the entity;

package de.incompleteco.spring.jpa.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="simple_entity")
public class SimpleEntity implements Serializable {

 private static final long serialVersionUID = 1l;

 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Long id;
 
 @Column
 private String stuff;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getStuff() {
  return stuff;
 }

 public void setStuff(String stuff) {
  this.stuff = stuff;
 }

 @Override
 public String toString() {
  return "SimpleEntity [id=" + id + ", stuff=" + stuff + "]";
 }
 
}


here's the entity manager factory;



 
  
  
  
   
    
   
  
 
 
 
  

  
   
   
  

 



here's the Spring Integration for the JPA service



 
 
 
 
  
 
 
 
 
 
 
   
 

 
 
 
 
   
 
 



here's the Spring Integration for the HTTP endpoint



 
 
 
 
 

 
  
  
 

 
 
 
 
  
  
 
 
 
 
 
  
  
  
  
 

 
 
 

 




and here's a test showing it all

package de.incompleteco.spring.jpa;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;

import de.incompleteco.spring.jpa.domain.SimpleEntity;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:/META-INF/spring/*-context.xml"})
@ActiveProfiles("junit")
public class IntegrationTest {

 @Resource
 private WebApplicationContext context;
 
 private ObjectMapper mapper;
 
 private MockMvc mvc;
 
 @Before
 public void before() throws Exception {
  //setup
  mapper = new ObjectMapper();//json
  mvc = webAppContextSetup(context).build();  
 }

 @Test
 public void test() throws Exception {
  //create an object
  SimpleEntity entity = new SimpleEntity();
  entity.setStuff("hello world");
  //convert
  String json = mapper.writeValueAsString(entity);
  //execute
  MvcResult result = mvc.perform(put("/simpleEntity").contentType(MediaType.APPLICATION_JSON).content(json))
   .andExpect(status().isOk()).andReturn();
  //now convert back
  SimpleEntity resultEntity = mapper.readValue(result.getResponse().getContentAsByteArray(), SimpleEntity.class);
  //now check
  assertTrue(resultEntity.getId() > 0);
  assertEquals(entity.getStuff(),resultEntity.getStuff());
  //retrieve by the id
  result = mvc.perform(get("/simpleEntity").param("id", resultEntity.getId().toString()))
   .andExpect(status().isOk()).andReturn();
  //now convert
  SimpleEntity retrievedEntity = mapper.readValue(result.getResponse().getContentAsByteArray(), SimpleEntity.class);
  //now check
  assertEquals(entity.getStuff(),retrievedEntity.getStuff());
  //retrieve all
  result = mvc.perform(get("/simpleEntity"))
    .andExpect(status().isOk()).andReturn();
  //now it *should* be an array of SimpleEntity
  SimpleEntity[] entities = mapper.readValue(result.getResponse().getContentAsByteArray(),SimpleEntity[].class);
  //test
  assertNotNull(entities);
  assertTrue(entities.length > 0);
  
 }
}


if you want the code, it's here; https://github.com/incomplete-code/spring-integration-jpa