Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the Spring code #13

Merged
merged 3 commits into from
Mar 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions spring/src/main/java/hello/web/HelloDbController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
import org.springframework.web.bind.annotation.*;

@Controller
public class HelloDbController
public class HelloDbController
{

private static final int DB_ROWS = 10000;

@RequestMapping(value = "/db")
public Object index(HttpServletRequest request, HttpServletResponse response, Integer queries)
@RequestMapping(value = "/db", produces = "application/json")
@ResponseBody
public World[] index(Integer queries)
{
if (queries == null)
{
queries = 1;
}

final World[] worlds = new World[queries];
final Random random = ThreadLocalRandom.current();
final Session session = HibernateUtil.getSessionFactory().openSession();
Expand All @@ -41,17 +43,7 @@ public Object index(HttpServletRequest request, HttpServletResponse response, In
}

session.close();

try
{
new MappingJackson2HttpMessageConverter().write(
worlds, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
}
catch (IOException e)
{
// do nothing
}

return null;

return worlds;
}
}
41 changes: 25 additions & 16 deletions spring/src/main/java/hello/web/HelloJsonController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,36 @@
import org.springframework.http.server.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

/**
* Handles requests for the application home page.
*/
@Controller
public class HelloJsonController {

@RequestMapping(value = "/json")
public Object json(HttpServletResponse response)
public class HelloJsonController
{

@RequestMapping(value = "/json", produces = "application/json")
@ResponseBody
public Message json()
{
return new Message("Hello, world");
}

public static class Message
{
Map<String, String> json = new HashMap<String, String>();
json.put("message", "Hello, world");

try {
new MappingJackson2HttpMessageConverter().write(
json, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
} catch (HttpMessageNotWritableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

private final String message;

public Message(String message)
{
this.message = message;
}

public String getMessage()
{
return message;
}
return null;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

Expand Down