Skip to content

Commit 2b7f03e

Browse files
author
eugenp
committed
adding back the missing code
1 parent 2d2cefa commit 2b7f03e

27 files changed

Lines changed: 1696 additions & 0 deletions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.baeldung.config;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.List;
5+
6+
import org.baeldung.config.converter.KryoHttpMessageConverter;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.http.converter.HttpMessageConverter;
10+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
11+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
12+
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
13+
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
14+
import org.springframework.oxm.xstream.XStreamMarshaller;
15+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
16+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
17+
18+
@Configuration
19+
@EnableWebMvc
20+
@ComponentScan({ "org.baeldung.web" })
21+
public class WebConfig extends WebMvcConfigurerAdapter {
22+
23+
public WebConfig() {
24+
super();
25+
}
26+
27+
//
28+
29+
@Override
30+
public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
31+
messageConverters.add(createXmlHttpMessageConverter());
32+
// messageConverters.add(new MappingJackson2HttpMessageConverter());
33+
34+
final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
35+
builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm"));
36+
messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
37+
// messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
38+
messageConverters.add(new ProtobufHttpMessageConverter());
39+
messageConverters.add(new KryoHttpMessageConverter());
40+
super.configureMessageConverters(messageConverters);
41+
}
42+
43+
private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
44+
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
45+
46+
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
47+
xmlConverter.setMarshaller(xstreamMarshaller);
48+
xmlConverter.setUnmarshaller(xstreamMarshaller);
49+
50+
return xmlConverter;
51+
}
52+
53+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.config.converter;
2+
3+
import java.io.IOException;
4+
5+
import org.baeldung.web.dto.Foo;
6+
import org.springframework.http.HttpInputMessage;
7+
import org.springframework.http.HttpOutputMessage;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.http.converter.AbstractHttpMessageConverter;
10+
11+
import com.esotericsoftware.kryo.Kryo;
12+
import com.esotericsoftware.kryo.io.Input;
13+
import com.esotericsoftware.kryo.io.Output;
14+
15+
/**
16+
* An {@code HttpMessageConverter} that can read and write Kryo messages.
17+
*/
18+
public class KryoHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
19+
20+
public static final MediaType KRYO = new MediaType("application", "x-kryo");
21+
22+
private static final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>() {
23+
@Override
24+
protected Kryo initialValue() {
25+
final Kryo kryo = new Kryo();
26+
kryo.register(Foo.class, 1);
27+
return kryo;
28+
}
29+
};
30+
31+
public KryoHttpMessageConverter() {
32+
super(KRYO);
33+
}
34+
35+
@Override
36+
protected boolean supports(final Class<?> clazz) {
37+
return Object.class.isAssignableFrom(clazz);
38+
}
39+
40+
@Override
41+
protected Object readInternal(final Class<? extends Object> clazz, final HttpInputMessage inputMessage) throws IOException {
42+
final Input input = new Input(inputMessage.getBody());
43+
return kryoThreadLocal.get().readClassAndObject(input);
44+
}
45+
46+
@Override
47+
protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException {
48+
final Output output = new Output(outputMessage.getBody());
49+
kryoThreadLocal.get().writeClassAndObject(output, object);
50+
output.flush();
51+
}
52+
53+
@Override
54+
protected MediaType getDefaultContentType(final Object object) {
55+
return KRYO;
56+
}
57+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.baeldung.web.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
@Controller
10+
@RequestMapping(value = "/ex")
11+
public class BarMappingExamplesController {
12+
13+
public BarMappingExamplesController() {
14+
super();
15+
}
16+
17+
// API
18+
19+
// with @RequestParam
20+
21+
@RequestMapping(value = "/bars")
22+
@ResponseBody
23+
public String getBarBySimplePathWithRequestParam(@RequestParam("id") final long id) {
24+
return "Get a specific Bar with id=" + id;
25+
}
26+
27+
@RequestMapping(value = "/bars", params = "id")
28+
@ResponseBody
29+
public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") final long id) {
30+
return "Get a specific Bar with id=" + id;
31+
}
32+
33+
@RequestMapping(value = "/bars", params = { "id", "second" })
34+
@ResponseBody
35+
public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") final long id) {
36+
return "Get a specific Bar with id=" + id;
37+
}
38+
39+
// with @PathVariable
40+
41+
@RequestMapping(value = "/bars/{numericId:[\\d]+}")
42+
@ResponseBody
43+
public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) {
44+
return "Get a specific Bar with id=" + numericId;
45+
}
46+
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.web.controller;
2+
3+
import org.baeldung.web.dto.Company;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class CompanyController {
10+
11+
@RequestMapping(value = "/companyRest", produces = MediaType.APPLICATION_JSON_VALUE)
12+
public Company getCompanyRest() {
13+
final Company company = new Company(1, "Xpto");
14+
return company;
15+
}
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.baeldung.web.controller;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
5+
6+
import org.baeldung.web.dto.Foo;
7+
import org.baeldung.web.dto.FooProtos;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.ResponseBody;
15+
import org.springframework.web.bind.annotation.ResponseStatus;
16+
17+
@Controller
18+
public class FooController {
19+
20+
public FooController() {
21+
super();
22+
}
23+
24+
// API - read
25+
26+
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
27+
@ResponseBody
28+
public Foo findById(@PathVariable final long id) {
29+
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
30+
}
31+
32+
// API - write
33+
34+
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
35+
@ResponseStatus(HttpStatus.OK)
36+
@ResponseBody
37+
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
38+
System.out.println(foo);
39+
return foo;
40+
}
41+
42+
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
43+
@ResponseBody
44+
public FooProtos.Foo findProtoById(@PathVariable final long id) {
45+
return FooProtos.Foo.newBuilder().setId(1).setName("Foo Name").build();
46+
}
47+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.baeldung.web.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
@Controller
10+
@RequestMapping(value = "/ex")
11+
public class FooMappingExamplesController {
12+
13+
public FooMappingExamplesController() {
14+
super();
15+
}
16+
17+
// API
18+
19+
// mapping examples
20+
21+
@RequestMapping(value = "/foos")
22+
@ResponseBody
23+
public String getFoosBySimplePath() {
24+
return "Simple Get some Foos";
25+
}
26+
27+
// with @PathVariable
28+
29+
@RequestMapping(value = "/foos/{id}")
30+
@ResponseBody
31+
public String getFoosBySimplePathWithPathVariable(@PathVariable final long id) {
32+
return "Get a specific Foo with id=" + id;
33+
}
34+
35+
@RequestMapping(value = "/foos/{fooid}/bar/{barid}")
36+
@ResponseBody
37+
public String getFoosBySimplePathWithPathVariables(@PathVariable final long fooid, @PathVariable final long barid) {
38+
return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid;
39+
}
40+
41+
// other HTTP verbs
42+
43+
@RequestMapping(value = "/foos", method = RequestMethod.POST)
44+
@ResponseBody
45+
public String postFoos() {
46+
return "Post some Foos";
47+
}
48+
49+
// with headers
50+
51+
@RequestMapping(value = "/foos", headers = "key=val")
52+
@ResponseBody
53+
public String getFoosWithHeader() {
54+
return "Get some Foos with Header";
55+
}
56+
57+
@RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" })
58+
@ResponseBody
59+
public String getFoosWithHeaders() {
60+
return "Get some Foos with Header";
61+
}
62+
63+
// @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json")
64+
// @ResponseBody
65+
// public String getFoosAsJsonFromBrowser() {
66+
// return "Get some Foos with Header Old";
67+
// }
68+
69+
@RequestMapping(value = "/foos", produces = { "application/json", "application/xml" })
70+
@ResponseBody
71+
public String getFoosAsJsonFromREST() {
72+
return "Get some Foos with Header New";
73+
}
74+
75+
// advanced - multiple mappings
76+
77+
@RequestMapping(value = { "/advanced/bars", "/advanced/foos" })
78+
@ResponseBody
79+
public String getFoosOrBarsByPath() {
80+
return "Advanced - Get some Foos or Bars";
81+
}
82+
83+
@RequestMapping(value = "*")
84+
@ResponseBody
85+
public String getFallback() {
86+
return "Fallback for GET Requests";
87+
}
88+
89+
@RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST })
90+
@ResponseBody
91+
public String allFallback() {
92+
return "Fallback for All Requests";
93+
}
94+
95+
@RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST })
96+
@ResponseBody
97+
public String putAndPostFoos() {
98+
return "Advanced - PUT and POST within single method";
99+
}
100+
101+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.web.controller;
2+
3+
import java.util.Date;
4+
5+
import org.baeldung.web.dto.Item;
6+
import org.baeldung.web.dto.ItemManager;
7+
import org.baeldung.web.dto.Views;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import com.fasterxml.jackson.annotation.JsonView;
13+
14+
@RestController
15+
public class ItemController {
16+
17+
@JsonView(Views.Public.class)
18+
@RequestMapping("/items/{id}")
19+
public Item getItemPublic(@PathVariable final int id) {
20+
return ItemManager.getById(id);
21+
}
22+
23+
@JsonView(Views.Internal.class)
24+
@RequestMapping("/items/internal/{id}")
25+
public Item getItemInternal(@PathVariable final int id) {
26+
return ItemManager.getById(id);
27+
}
28+
29+
@RequestMapping("/date")
30+
public Date getCurrentDate() {
31+
return new Date();
32+
}
33+
}

0 commit comments

Comments
 (0)