Last active
August 30, 2015 22:46
-
-
Save brunocribeiro/1e8a05af72c8f8455293 to your computer and use it in GitHub Desktop.
Spring MVC REST Service Sample
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
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:p="http://www.springframework.org/schema/p" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd | |
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> | |
<context:component-scan base-package="com.brunocesar" /> | |
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> | |
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> | |
<property name="defaultContentType" value="application/json" /> | |
<property name="mediaTypes"> | |
<value> | |
json=application/json | |
xml=application/xml | |
</value> | |
</property> | |
</bean> | |
</beans> |
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
package com.brunocesar.bean; | |
public class Candidato { | |
public Candidato() {} | |
public Candidato(final long id, final String nome) { | |
this.id = id; | |
this.nome = nome; | |
} | |
private long id; | |
private String nome; | |
public long getId() { | |
return id; | |
} | |
public void setId(final long id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(final String nome) { | |
this.nome = nome; | |
} | |
} |
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
package com.brunocesar.service; | |
import java.util.List; | |
import com.brunocesar.bean.Candidato; | |
public interface CandidatoService { | |
List<Candidato> listarCandidatos(); | |
} |
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
package com.brunocesar.service.impl; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.springframework.stereotype.Service; | |
import com.brunocesar.bean.Candidato; | |
import com.brunocesar.service.CandidatoService; | |
@Service | |
public class CandidatoServiceImpl implements CandidatoService { | |
@Override | |
public List<Candidato> listarCandidatos() { | |
final List<Candidato> result = new ArrayList<>(); | |
result.add(new Candidato(1, "Pedro da Silva")); | |
result.add(new Candidato(2, "Paula Pereira")); | |
result.add(new Candidato(3, "<anonimo>")); | |
return result; | |
} | |
} |
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
package com.brunocesar.ws; | |
import java.util.List; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.brunocesar.bean.Candidato; | |
import com.brunocesar.service.CandidatoService; | |
@RestController | |
@RequestMapping(value = "/test") | |
public class CandidatosWS { | |
@Autowired | |
private CandidatoService candidatoService; | |
@RequestMapping(value = "/candidatos", method = RequestMethod.GET) | |
public List<Candidato> allCandidatos() { | |
return candidatoService.listarCandidatos(); | |
} | |
} |
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
package com.brunocesar.config; | |
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; | |
public class SpringMvcWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { | |
@Override | |
protected Class<?>[] getRootConfigClasses() { | |
return new Class[] {WebApplicationConfig.class}; | |
} | |
@Override | |
protected Class<?>[] getServletConfigClasses() { | |
return null; | |
} | |
@Override | |
protected String[] getServletMappings() { | |
return new String[] {"/"}; | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |
metadata-complete="true"> | |
<display-name>Test Application</display-name> | |
<listener> | |
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> | |
</listener> | |
<listener> | |
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> | |
</listener> | |
<!-- spring servlet --> | |
<servlet> | |
<servlet-name>springDispatcherServlet</servlet-name> | |
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |
<init-param> | |
<param-name>contextConfigLocation</param-name> | |
<param-value>WEB-INF/applicationContext-mvc.xml</param-value> | |
</init-param> | |
<load-on-startup>0</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>springDispatcherServlet</servlet-name> | |
<url-pattern>/</url-pattern> | |
</servlet-mapping> | |
</web-app> |
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
package com.brunocesar.config; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.MediaType; | |
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; | |
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
@EnableWebMvc | |
@Configuration | |
@ComponentScan(basePackages = "com.brunocesar") | |
public class WebApplicationConfig extends WebMvcConfigurerAdapter { | |
@Override | |
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { | |
configurer.parameterName("mediaType") | |
.defaultContentType(MediaType.APPLICATION_JSON) | |
.mediaType("xml", MediaType.APPLICATION_XML) | |
.mediaType("json", MediaType.APPLICATION_JSON); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment